Last active
August 21, 2021 07:08
-
-
Save RayPS/792c1b17812b17d24711b61254191060 to your computer and use it in GitHub Desktop.
Custom UIFont with font-family/font-weight/font-style/font-size by UIFontDescriptor
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
// Put font files to Xcode project. | |
// Add this properties to Info.plist: | |
// Fonts provided by application | |
// Item 0: SF-Pro-Display-Regular.otf | |
// Item 1: SF-Pro-Display-Bold.otf | |
// Item 2: ... | |
// Tips: To get the list of iOS SDK built-in fonts you can found on http://iosfonts.com | |
// Or type "po [UIFont familyNames]" in Xcode lldb console. | |
// The following strings are Family names, not PostScript name or Full name. | |
let fonts = ["SF Pro Display", "Georgia", "Avenir Next", "Palatino"] | |
let fontIndex = 0 | |
let fontBold = true | |
let fontItalic = true | |
let fontSize = 32 | |
let descriptor = UIFontDescriptor(fontAttributes: | |
[ | |
UIFontDescriptorFamilyAttribute: fonts[fontIndex], | |
UIFontDescriptorTraitsAttribute: [ | |
UIFontWeightTrait: fontBold ? UIFontWeightBold : UIFontWeightRegular, | |
UIFontSlantTrait: fontItalic ? 1.0 : 0.0 | |
] | |
] | |
) | |
// Another working implement: | |
// let descriptor = UIFontDescriptor(name: "Avenir Next", size: 32).withSymbolicTraits([.traitBold, .traitItalic]) | |
myUILabel.font = UIFont(descriptor: descriptor, size: fontSize) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment