Skip to content

Instantly share code, notes, and snippets.

@ablyGurjitSingh
Created March 31, 2021 12:01
Show Gist options
  • Save ablyGurjitSingh/4b30668de6956f8a619af2817d0c1568 to your computer and use it in GitHub Desktop.
Save ablyGurjitSingh/4b30668de6956f8a619af2817d0c1568 to your computer and use it in GitHub Desktop.
Swift System Italic font with weights Helper.
extension UIFont {
class func italicSystemFont(ofSize size: CGFloat, weight: UIFont.Weight = .regular)-> UIFont {
let font = UIFont.systemFont(ofSize: size, weight: weight)
switch weight {
case .ultraLight, .light, .thin, .regular:
return font.withTraits(.traitItalic, ofSize: size)
case .medium, .semibold, .bold, .heavy, .black:
return font.withTraits(.traitBold, .traitItalic, ofSize: size)
default:
return UIFont.italicSystemFont(ofSize: size)
}
}
func withTraits(_ traits: UIFontDescriptor.SymbolicTraits..., ofSize size: CGFloat) -> UIFont {
let descriptor = self.fontDescriptor
.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits))
return UIFont(descriptor: descriptor!, size: size)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment