Created
June 15, 2017 12:23
-
-
Save Marcocanc/f6b95e3d3af895be91f0dacfec3cf1c5 to your computer and use it in GitHub Desktop.
Custom Fonts in iOS
This file contains 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
import UIKit | |
extension UIFont { | |
/// Returns the font in the specified size and weight. Default weight is `regular` | |
open class func font(ofSize fontSize: CGFloat, weight: FontWeight = .regular) -> UIFont { | |
let fontName = "UniversNextPro-\(weight.rawValue)" | |
guard let font = UIFont(name: fontName, size: fontSize) else { | |
fatalError("\(fontName) not found") | |
} | |
return font | |
} | |
/// Returns the custom font with monospaced digits in the specified size and weight. Default weight is `regular` | |
open class func monospacedDigitFont(ofSize fontSize: CGFloat, weight: FontWeight = .regular) -> UIFont { | |
let font = self.font(ofSize: fontSize, weight: weight) | |
let fontDescriptorFeatureSettings = [ | |
[ | |
UIFontFeatureTypeIdentifierKey: kNumberSpacingType, | |
UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector | |
] | |
] | |
let fontDescriptorAttributes = [ | |
UIFontDescriptorFeatureSettingsAttribute: fontDescriptorFeatureSettings | |
] | |
let fontDescriptor = font.fontDescriptor.addingAttributes(fontDescriptorAttributes) | |
return UIFont(descriptor: fontDescriptor, size: fontDescriptor.pointSize) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment