Created
January 27, 2018 16:22
-
-
Save mansi-27/6efc46a05a49218bb26e797dc225fa88 to your computer and use it in GitHub Desktop.
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
| /// Get the Scaled version of your UIFont. | |
| /// | |
| /// - Parameters: | |
| /// - name: Name of the UIFont whose scaled version you wish to obtain. | |
| /// - textStyle: The text style for your font, i.e Body, Title etc... | |
| /// - Returns: The scaled UIFont version with the given textStyle | |
| func getScaledFont(forFont name: String, textStyle: UIFontTextStyle) -> UIFont { | |
| /// Uncomment the code below to check all the available fonts and have them printed in the console to double check the font name with existing fonts 😉 | |
| /*for family: String in UIFont.familyNames | |
| { | |
| print("\(family)") | |
| for names: String in UIFont.fontNames(forFamilyName: family) | |
| { | |
| print("== \(names)") | |
| } | |
| }*/ | |
| let userFont = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle) | |
| let pointSize = userFont.pointSize | |
| guard let customFont = UIFont(name: name, size: pointSize) else { | |
| fatalError(""" | |
| Failed to load the "\(name)" font. | |
| Make sure the font file is included in the project and the font name is spelled correctly. | |
| """ | |
| ) | |
| } | |
| return UIFontMetrics.default.scaledFont(for: customFont) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment