Created
November 25, 2019 19:15
-
-
Save henrik-dmg/3308818c70c05d1963e7703fde07fc3a to your computer and use it in GitHub Desktop.
A UIFont extension to enable dynamic for system font styles with any font weight
This file contains 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
import Foundation | |
import UIKit | |
public extension UIFont { | |
static func preferredFont(forTextStyle style: UIFont.TextStyle, weight: UIFont.Weight) -> UIFont { | |
let font = style.withWeight(weight) | |
let fontMetrics = UIFontMetrics(forTextStyle: style) | |
return fontMetrics.scaledFont(for: font) | |
} | |
} | |
public extension UIFont.TextStyle { | |
var baseFont: UIFont { | |
return UIFont.systemFont(ofSize: defaultPointSize, weight: defaultWeight) | |
} | |
func withWeight(_ weight: UIFont.Weight) -> UIFont { | |
return UIFont.systemFont(ofSize: defaultPointSize, weight: weight) | |
} | |
var defaultWeight: UIFont.Weight { | |
switch self { | |
case .headline: | |
return .semibold | |
default: | |
return .regular | |
} | |
} | |
var defaultPointSize: CGFloat { | |
switch self { | |
case .largeTitle: | |
return 34 | |
case .title1: | |
return 28 | |
case .title2: | |
return 22 | |
case .title3: | |
return 20 | |
case .headline: | |
return 17 | |
case .body: | |
return 17 | |
case .callout: | |
return 16 | |
case .subheadline: | |
return 15 | |
case .footnote: | |
return 13 | |
case .caption1: | |
return 12 | |
case .caption2: | |
return 11 | |
default: | |
return 17 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment