Skip to content

Instantly share code, notes, and snippets.

@mansi-27
Created January 27, 2018 16:22
Show Gist options
  • Select an option

  • Save mansi-27/6efc46a05a49218bb26e797dc225fa88 to your computer and use it in GitHub Desktop.

Select an option

Save mansi-27/6efc46a05a49218bb26e797dc225fa88 to your computer and use it in GitHub Desktop.
/// 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