Created
August 30, 2019 13:53
-
-
Save liamnichols/010c901b3bf3d621d8f3256c47b3b178 to your computer and use it in GitHub Desktop.
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 UIKit | |
extension UIFont { | |
/// Returns an instance of the system font for the specified text style and weight. | |
/// | |
/// The scale of the text style is determined by the specified traits. | |
/// | |
/// If no traits are specified (the default) the font will be scaled for the user's selected content size category, | |
/// | |
/// Otherwise the font will be scaled using the content size category information in the specified trait collection. | |
/// | |
/// - Parameters: | |
/// - style: The text style for which to return a font. See "Text Styles" in `UIFontDescriptor` for recognized values. | |
/// - traitCollection: The trait collection containing the content size category information. | |
/// - weight: The weight of the font, specified as a weight constant. For possible values see "Font Weights" in `UIFontDescriptor`. | |
/// - Returns: The system font associated with the specified text style with the specified weight. | |
class func preferredFont(forTextStyle style: TextStyle, | |
compatibleWith traitCollection: UITraitCollection? = nil, | |
weight: Weight) -> UIFont { | |
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style, compatibleWith: traitCollection) | |
let systemFont = UIFont.systemFont(ofSize: descriptor.pointSize, weight: weight) | |
let metrics = UIFontMetrics(forTextStyle: style) | |
return metrics.scaledFont(for: systemFont, compatibleWith: traitCollection) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment