This is the second article in a series that details the Open Source Work for iOS that I have been leading at YML.
Typography
The inspiration for the Y—Matter Type library came from the most common mistake I see when converting designs to code on iOS: overlooking line height. Designs define typographies (or text styles), which describe how text should be rendered on screen. The four basic parameters of a typography are:
- font family
- font weight
- font size
- line height
Font family: Helvetica Neue Font style: Regular Font size: 16px Line height: 24px
For example, a design might call for a regular weight Helvetica Neue font with 16 pt font size and 24 pt line height. The first three parameters tell you which font to load on iOS, but the fourth parameter is typically just ignored outright.
let font = UIFont(name: "HelveticaNeue", fontSize: 16)
The code above generates the correct font, but what is its line height? Is it 16
? It turns out that for a 16pt regular Helvetica Neue font, it is 18.64
. A UILabel
generated with that font will have a height of 19
on a 2x retina device (rounding up to the nearest pixel). But the designs expect that label to be 24
points tall per line of text. Of course you could constrain the label to have a height of 24
, but then it could not support Dynamic Type. Also if it contains multiple lines of text, you don’t necessarily know how many lines it will wrap to and so couldn’t possibly constrain the height correctly.
The way you get line height to render in UILabel
, UIButton
, UITextView
is by using paragraph styles and attributed strings. (To my knowledge SwiftUI does not yet support line height in any of its text views, which is one reason why I continue to utilize UIKit.) But applying paragraph styles via attributed strings to every button and label in an application would also be burdensome (which is why it typically just gets ignored).
To solve this, we created a Typography
model object that captures all the parameters of design system typography and then accurately renders text in UIKit (which can then be hosted in SwiftUI if necessary). In addition to the most common parameters listed above, Typography also supports:
- letter spacing (kerning)
- paragraph indent
- paragraph spacing
- text case (uppercase, lowercase, title case)
- text decoration (underline, strikethrough)
Typography
objects are determined by the design (and with proper transformation can be automatically generated from design files) and then rendered via subclasses for all the text-based UIView classes.
Y—Matter Type declares TypographyLabel
, TypographyButton
, TypographyTextField
, and TypographyTextView
classes to replace use of UILabel
, UIButton
, UITextField
, and UITextView
, respectively.
Accessibility Features
Typography
supports Dynamic Type scaling and the Accessibility Bold Text feature by default for any custom font.
Adopting Typography
is a bit of a paradigm shift because you no longer work with fonts, only typographies. That’s because the exact font chosen will be determined at run time based on the current trait collection. For example, the following label might actually use a Helvetica Medium font instead of Helvetica Regular if the user has enabled the Bold Text setting.
let body = Typography( familyName: "HelveticaNeue", fontWeight: .regular, fontSize: 16, lineHeight: 24 ) let label = TypographyLabel(typography: body)
The Bold Text feature works by having information about an entire font family (what weights are available) and not just a single font. If we know all the available weights, then we can vend the next heavier weight (if any) when Bold Text is enabled.
Dynamic Type scales Typography proportionately with line height. So the 16/24 Typography shown above scaled to 200% would use a 32pt font rendered with a 48pt line height.
Overview
The goals of the Y—Matter Type package are to:
- Support line height and other typographical properties (letter spacing, text decorations, text cases, and more) across labels, buttons, text fields, and text views
- Support Dynamic Type scaling and Accessibility Bold Text on custom fonts
- Accelerate accurate translation of Figma designs into code by having text-based elements with the exact same intrinsic size (based upon line height) as the corresponding elements in Figma
I am proud that we delivered low-level components that make it trivial to accurately render text as per designs while at the same time including Dynamic Type and Bold Text support with little additional burden for the developer. To my knowledge no client has ever asked for Bold Text support (or even knew it existed really), but we build that in for their users anyway.
Together with Y—CoreUI, Y—Matter Type is part of how we build iOS apps at YML.