Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get monospaced numbers in UILabel on iOS 9

At WWDC 2015, there was a session about the new “San Francisco” system font in iOS 9. It uses proportional number rendering instead of monospaced numbers by default when linked against the iOS 9 SDK. There is a convenient initializer on NSFont called NSFont.monospacedDigitsSystemFontOfSize(mySize weight:) that can be used to explicitly enable monospaced number display.

However I couldn't find the UIKit equivalent for this on UIFont.

like image 240
Samuel Mellert Avatar asked Jun 15 '15 20:06

Samuel Mellert


2 Answers

This is now available in UIFont since iOS 9:

+ (UIFont *)monospacedDigitSystemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight NS_AVAILABLE_IOS(9_0); 

eg:

[UIFont monospacedDigitSystemFontOfSize:42.0 weight:UIFontWeightMedium]; 

or in Swift:

UIFont.monospacedDigitSystemFont(ofSize: 42.0, weight: UIFontWeightMedium) 
like image 196
Ric Santos Avatar answered Sep 29 '22 04:09

Ric Santos


Handy UIFont extension:

extension UIFont {     var monospacedDigitFont: UIFont {         let newFontDescriptor = fontDescriptor.monospacedDigitFontDescriptor         return UIFont(descriptor: newFontDescriptor, size: 0)     } }  private extension UIFontDescriptor {     var monospacedDigitFontDescriptor: UIFontDescriptor {         let fontDescriptorFeatureSettings = [[UIFontDescriptor.FeatureKey.featureIdentifier: kNumberSpacingType,                                               UIFontDescriptor.FeatureKey.typeIdentifier: kMonospacedNumbersSelector]]         let fontDescriptorAttributes = [UIFontDescriptor.AttributeName.featureSettings: fontDescriptorFeatureSettings]         let fontDescriptor = self.addingAttributes(fontDescriptorAttributes)         return fontDescriptor     } } 

Usage with @IBOutlet properties:

@IBOutlet private var timeLabel: UILabel? {     didSet {         timeLabel.font = timeLabel.font.monospacedDigitFont     } } 

Latest version on GitHub.

like image 24
Rudolf Adamkovič Avatar answered Sep 29 '22 03:09

Rudolf Adamkovič