Skip to content

Instantly share code, notes, and snippets.

@konnnn
Last active February 22, 2023 01:07
Show Gist options
  • Save konnnn/9adf5394ad22c281bb68d76dd1a9a867 to your computer and use it in GitHub Desktop.
Save konnnn/9adf5394ad22c281bb68d76dd1a9a867 to your computer and use it in GitHub Desktop.
UIFont extension for San Francisco system font style selection
extension UIFont {
/// San Francisco System Font
/// - Parameters:
/// - fontSize: Font size
/// - weight: Font weight (Optional, by default: weight is regular)
/// - design: Font design: default, rounded, monospaced, serif (Optional, by default: design is default)
/// - Returns: System font of your size, weight and design
static func systemFont(ofSize fontSize: CGFloat, weight: UIFont.Weight = .regular, design: UIFontDescriptor.SystemDesign = .default) -> UIFont {
// Will be SF Compact or standard SF in case of failure.
if let descriptor = UIFont.systemFont(ofSize: fontSize, weight: weight).fontDescriptor.withDesign(design) {
return UIFont(descriptor: descriptor, size: fontSize)
} else {
return self.systemFont(ofSize: fontSize, weight: weight)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
label.font = UIFont.systemFont(ofSize: 22.0, weight: .bold, design: .rounded)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment