Skip to content

Geri-Borbas/iOS.Blog.UILabel_Typography_Extensions

Repository files navigation

UILabel Typography Extensions πŸ“

Set UILabel line height, letter spacing (and more).

let headerLabel = UILabel()
headerLabel.textColor = UIColor(named: "Mars")
headerLabel.font = UIFont(name: "HelveticaNeue-CondensedBlack", size: 100)
headerLabel.lineHeight = 80
headerLabel.letterSpacing = 100 * -0.02
headerLabel.text = "Mars"

See the complementary article at UILabel line height, letter spacing and more UILabel typography extensions.

How it works

It is an extension on UILabel that adds some typographic properties using Objective-C Runtime (for stored properties on a Swift extension), NSAttributedString (for manage typographic properties), and NSKeyValueObservation (to be able to use the regular text property to manage text content).

It also takes care of baselineOffset values to keep the text content in the vertical center (right). By default, NSAttributedString sticks multiline label content to the top of the container view (left), which makes it hard to match the UI with the design specifications.

Using this extension you can build screens that 100% match design specifications. More on that in the article at UILabel line height, letter spacing and more UILabel typography extensions.

License

Licensed under the MIT License.