Skip to content

Instantly share code, notes, and snippets.

@Marcocanc
Created June 15, 2017 12:23
Show Gist options
  • Save Marcocanc/f6b95e3d3af895be91f0dacfec3cf1c5 to your computer and use it in GitHub Desktop.
Save Marcocanc/f6b95e3d3af895be91f0dacfec3cf1c5 to your computer and use it in GitHub Desktop.
Custom Fonts in iOS
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