Skip to main contentSkip to docs navigation

Chassis

iOS and Android

View on GitHub

Draw the icons of a set in an iOS or Android app, from the ios and android targets of the build and the archives of a release.

Introduction

The build writes the files of a set for an app besides those of the web, when the configuration of the set names the platform in its targets. The ios target writes an asset catalog and a Swift file, and the android target writes a vector drawable per icon. The release of the set attaches them to its GitHub release as an archive per platform, which an app adds to its project. On both platforms an icon is a template image, which takes the tint of its view as an icon takes currentcolor on the web.

The examples use the set of the team Acme: the font acme-glyphs, the prefix ag, the package @acme/glyphs, and the icon bar-fill, drawn on a frame of 16. For your own set, use the names that the build writes from your font name and your prefix, which Names per platform lists.

Installation

An app gets the files of a set from the archives of a release, or from the folders of a build. Both need a set whose configuration names the targets.

The targets

The web is always built, and chassis.build.targets names the other targets of the set, ios and android, none by default. init does not write the setting, so add it to chassis.build of package.json in the repository of icons:

JSON
{
  "chassis": {
    "build": {
      "name": "acme-glyphs",
      "prefix": "ag",
      "targets": ["ios", "android"]
    }
  }
}

Then build, and commit package.json. The build writes dist/ios/ for ios, and dist/android/ for android. dist/ is not committed: the release of the set builds it again, and writes an archive of each folder.

TEXT
dist/
  ios/AcmeGlyphs.xcassets/                       -> The asset catalog: one image set per icon
  ios/AcmeGlyphs.xcassets/bar-fill.imageset/     -> bar-fill.svg and its Contents.json
  ios/AcmeGlyphs.swift                           -> The enum AgIcon, and the initializers of UIImage and Image
  android/res/drawable/ag_bar_fill.xml           -> One vector drawable per icon

The name of the catalog and of the Swift file is the font name in PascalCase, AcmeGlyphs, and the enum of the icons is the prefix in PascalCase with Icon, AgIcon. Removing a target from the list removes its folder from dist/ with the next build.

The archives

Each release of a set on GitHub has an archive per target beside the archive of the web: <font>-ios-<version>.zip holds the files of dist/ios/, and <font>-android-<version>.zip the files of dist/android/, both at their top level. Download them from the releases of the repository of the set. The npm package of the set does not hold them: it holds the output for the web.

For iOS, unpack the archive, and add AcmeGlyphs.xcassets and AcmeGlyphs.swift to one target of the app in Xcode. The Swift file is then part of the module of that target, so it needs no import there, and it finds the catalog in the bundle of that target.

For Android, unpack the archive into src/main/ of a module, so that its res/ joins the resources of the module:

Shell
unzip acme-glyphs-android-<version>.zip -d app/src/main/

A set of your own that is not released yet has the same files in dist/ios/ and dist/android/ of its repository after a build, and npm run archive writes the same archives into archives/.

Basic usage

An icon is a case of the enum on iOS, and a drawable of R.drawable on Android. The names follow the name of the icon, so bar-fill is AgIcon.barFill and R.drawable.ag_bar_fill.

iOS

Draw an icon with SwiftUI Image or with UIImage, in a file of the target that holds AcmeGlyphs.swift:

SWIFT
Image(AgIcon.barFill)
UIImage(AgIcon.barFill)

The raw value of a case is the name of the icon, and AgIcon.allCases lists every icon of the set. A name that is a word of Swift becomes a case between backticks.

Android

Draw an icon with Icon of Jetpack Compose, or with an ImageView in a layout:

KOTLIN
Icon(painterResource(R.drawable.ag_bar_fill), contentDescription = null)
XML
<ImageView
    android:layout_width="24dp"
    android:layout_height="24dp"
    app:srcCompat="@drawable/ag_bar_fill"
    app:tint="?attr/colorOnSurface" />

Size and color

An icon is a vector at the size of the frame, in points on iOS and in dp on Android, and it scales without blur. Its color comes from the view, as on the web it comes from the text around it.

Template images

Each image set of the catalog has "template-rendering-intent": "template" and "preserves-vector-representation": true in its Contents.json, which the build writes:

JSON
{
  "images": [
    {
      "filename": "bar-fill.svg",
      "idiom": "universal"
    }
  ],
  "info": {
    "author": "xcode",
    "version": 1
  },
  "properties": {
    "preserves-vector-representation": true,
    "template-rendering-intent": "template"
  }
}

So an image of the set draws in the foreground style of a SwiftUI view and in the tintColor of a UIImageView. Give it another size with resizable() and a frame:

SWIFT
Image(AgIcon.barFill)
    .resizable()
    .frame(width: 24, height: 24)
    .foregroundStyle(.red)
SWIFT
let imageView = UIImageView(image: UIImage(AgIcon.barFill))
imageView.tintColor = .systemBlue

Tinted drawables

A drawable fills its paths with black, which a tint replaces. The build writes it from the optimized SVG file of the icon:

XML
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="16dp"
    android:height="16dp"
    android:viewportWidth="16"
    android:viewportHeight="16">
    <path
        android:fillColor="#FF000000"
        android:pathData="M2 5h12v6H2z"/>
</vector>

Icon of Compose tints with the content color of its place, or with its tint parameter, and takes another size from a modifier such as Modifier.size(24.dp). An ImageView draws the black of the file unless it has a tint, so give it app:tint as the example of Basic usage does.

Accessibility

An icon beside a label is decorative, and an icon alone carries the meaning of its control, so a screen reader has to name it.

On iOS, hide a decorative icon with accessibilityHidden(true), and name one that carries meaning with accessibilityLabel. SwiftUI otherwise reads the name of the image, such as bar-fill. A UIImageView is not read unless isAccessibilityElement is true and it has an accessibilityLabel.

SWIFT
Image(AgIcon.barFill)
    .accessibilityLabel("Settings")

On Android, contentDescription = null leaves a decorative icon out, and a string names one that carries meaning. In a layout, android:contentDescription names it, and android:importantForAccessibility="no" leaves it out.

Customization

The names of an icon on each platform come from its name, the font name and the prefix of the set, and the build writes them for any set.

Names per platform

The web, iOS and Android name an icon in the form each one allows. For the icon bar-fill of the set acme-glyphs:

PlatformFormFor bar-fill
WebThe class <prefix>-<name>, the symbol #<name>, the file <name>.svgag-bar-fill
iOSThe image set <name>, and the name in camelCase as a case of the enum, which is the prefix in PascalCase with IconAgIcon.barFill
AndroidThe drawable <prefix>_<name>, the class with _R.drawable.ag_bar_fill

For a set of your own, the same forms follow from your font name and your prefix: the font name acme-icons and the prefix ai give the catalog AcmeIcons.xcassets, the enum AiIcon, and the drawables ai_<name>.

Icons outside a set

Chassis Assets delivers the fonts and images of the apps of a brand, and an icon that is not in a set, such as one icon of one app. The Icon Assets page of Chassis Assets documents it. The icons of a set come from the targets of its build, so do not copy them into Chassis Assets: take the archives of a release.

Best practices

The files of a target are the output of one build of the set, and an app reads them by name. These practices keep the names and the files in step.

  • Turn on the targets in the repository of the set: chassis.build.targets writes the files with every build, and every release attaches their archives.
  • Take a release, not a build: the archives of a release hold the files of the version of its tag, v<version>, and its changelog entry names the icons that it adds, renames or removes.
  • Tint every ImageView: a drawable is black until a tint replaces it.
  • Name an icon by its case or its resource: AgIcon.barFill and R.drawable.ag_bar_fill fail to compile when the icon is removed or renamed, where a string fails when the app runs.

Avoid the habits that undo this:

  • Do not edit the files of dist/ios/ or dist/android/: the next build writes them again from source/. Change the icon in the repository of the set.
  • Do not mix the files of two versions: the catalog, the Swift file and the drawables of one release name the same icons. Unpacking an archive over that of an earlier version keeps the drawables of the icons that it removed, so remove the files of the earlier archive first.

Troubleshooting

Each entry gives the message or the symptom, then the cause and the fix.

Step has nothing to do

build --only ios stops with "targets" of chassis.build does not name ios, so the step has nothing to do. The configuration does not name the target. Add it to chassis.build.targets, as The targets shows, and build.

Targets are refused

The build stops with "targets" has to be a list of ios, android, and is <value>. The web is always built. The list names web, a platform the build does not know, or a target twice. Keep ios and android, each once.

Two icons, one case

The build stops with The icons "<name>" and "<name>" would both be the Swift case <case>: rename one. Two names differ only in their hyphens, such as bar-2-fill and bar2-fill, and give one case of the enum. Rename one of the icons in source/, which is the decision of the maintainer of the set.

Image is not found

UIImage(AgIcon.barFill) stops the app, or Image(AgIcon.barFill) draws nothing. The Swift file looks for the catalog in the bundle of its own target, and AcmeGlyphs.xcassets was added to another target, or to none. Add the catalog to the target that holds AcmeGlyphs.swift.

Icon draws black

An icon draws in black on Android. The view has no tint, and the drawable fills its paths with black. Give the ImageView an app:tint, or draw the icon with Icon of Compose, which tints it.

Next steps

Continue with one of these pages: