Skip to content

Instantly share code, notes, and snippets.

@RickyVaughn2
Last active June 29, 2018 13:10
Show Gist options
  • Save RickyVaughn2/7935db3a222f1adfef4c5e98c8e39830 to your computer and use it in GitHub Desktop.
Save RickyVaughn2/7935db3a222f1adfef4c5e98c8e39830 to your computer and use it in GitHub Desktop.
Swift 3 Monospaced UIFont Extension
// monospaced extension
// usage: object.font = object.font.monospacedDigitFont
import UIKit
extension UIFont {
var monospacedDigitFont: UIFont {
let oldFontDescriptor = fontDescriptor
let newFontDescriptor = oldFontDescriptor.monospacedDigitFontDescriptor
return UIFont(descriptor: newFontDescriptor, size: 0)
}
}
private extension UIFontDescriptor {
var monospacedDigitFontDescriptor: UIFontDescriptor {
let fontDescriptorFeatureSettings = [[UIFontFeatureTypeIdentifierKey: kNumberSpacingType, UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector]]
let fontDescriptorAttributes = [UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings]
let fontDescriptor = self.addingAttributes(fontDescriptorAttributes)
return fontDescriptor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment