Last active
June 29, 2018 13:10
-
-
Save RickyVaughn2/7935db3a222f1adfef4c5e98c8e39830 to your computer and use it in GitHub Desktop.
Swift 3 Monospaced UIFont Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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