Skip to content

Instantly share code, notes, and snippets.

@modestman
Created April 1, 2021 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modestman/90becd7190c0e7e72b95c474dda518c4 to your computer and use it in GitHub Desktop.
Save modestman/90becd7190c0e7e72b95c474dda518c4 to your computer and use it in GitHub Desktop.
iOS Fonts
import UIKit
extension UIFont {
static var largeTitle: UIFont {
basisSFPro(FontFamily.SFProText.bold, size: 40, iPhone5Size: 28, textStyle: .largeTitle)
}
static var title1: UIFont {
basisSFPro(FontFamily.SFProText.bold, size: 24, iPhone5Size: 20, textStyle: .title1)
}
static var body: UIFont {
basisSFPro(FontFamily.SFProText.regular, size: 18, iPhone5Size: 16, textStyle: .body)
}
static var bodyBold: UIFont {
basisSFPro(FontFamily.SFProText.bold, size: 18, iPhone5Size: 16, textStyle: .body)
}
static var caption1: UIFont {
basisSFPro(FontFamily.SFProText.regular, size: 14, iPhone5Size: 12, textStyle: .caption1)
}
static var caption2: UIFont {
basisSFPro(FontFamily.SFProText.regular, size: 12, iPhone5Size: 11, textStyle: .caption2)
}
static var headline: UIFont {
basisSFPro(FontFamily.SFProText.semibold, size: 18, iPhone5Size: 16, textStyle: .headline)
}
static var number: UIFont {
basisSFPro(FontFamily.SFProText.bold, size: 80, iPhone5Size: 80, textStyle: .largeTitle, scaled: false)
}
private static func basisSFPro(
_ family: FontConvertible,
size: CGFloat,
iPhone5Size: CGFloat,
textStyle: UIFont.TextStyle,
scaled: Bool = true) -> UIFont {
var actualSize = size
// Для телефонов с маленьким размером экрана (iPhone 5, SE) используем другой размер шрифта
if UIScreen.main.bounds.width == 320 {
actualSize = iPhone5Size
}
guard let font = family.font(size: actualSize) else {
print("Warning: \(family.name) not found.")
return UIFont.systemFont(ofSize: size, weight: .regular)
}
if scaled {
let metrics = UIFontMetrics(forTextStyle: textStyle)
return metrics.scaledFont(for: font)
} else {
return font
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment