This file contains hidden or 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
| label.font = UIFont.preferedFont(forTextStyle: .body) |
This file contains hidden or 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
| if #available(iOS 10.0, *) { | |
| label.adjustsFontForContentSizeCategory = true | |
| } |
This file contains hidden or 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
| private func adjustForContentSizeCategoryChanges() { | |
| if #available(iOS 10.0, *) { | |
| label.adjustsFontForContentSizeCategory = true | |
| } else { | |
| NotificationCenter.default.addObserver(self, | |
| selector: #selector(self.didChangeContentSizeCategory), | |
| name: UIContentSizeCategory.didChangeNotification, | |
| object: nil) | |
| } | |
| } |
This file contains hidden or 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
| if #available(iOS 11.0, *) { | |
| if let font = UIFont(name: "custom", size: 20.0) { | |
| label.font = UIFontMetrics(forTextStyle: .title1).scaledFont(for: font) | |
| } | |
| } |
This file contains hidden or 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
| enum jurassicPark: String { | |
| case | |
| regular = "JurassicPark-Regular", | |
| semibold = "JurassicPark-Semibold", | |
| bold = "JurassicPark-Bold" | |
| } |
This file contains hidden or 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
| protocol ScalableFont { | |
| func scaled(forTextStyle textStyle: UIFont.TextStyle?, maxSize: CGFloat?) -> UIFont | |
| } | |
| extension UIFont: ScalableFont { | |
| func scaled(forTextStyle textStyle: UIFont.TextStyle? = nil, maxSize: CGFloat? = nil) -> UIFont { | |
| let fontMetrics = textStyle != nil ? UIFontMetrics(forTextStyle: textStyle!) : UIFontMetrics.default | |
| if let maxSize = maxSize { | |
| return fontMetrics.scaledFont(for: self, maximumPointSize: maxSize) | |
| } |
This file contains hidden or 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
| extension UIFont { | |
| enum jurassicPark: String, CustomFont { | |
| case | |
| regular = "JurassicPark-Regular", | |
| semibold = "JurassicPark-Semibold", | |
| bold = "JurassicPark-Bold" | |
| } | |
| } |
This file contains hidden or 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
| extension Optional where Wrapped == UIFont { | |
| var unwrapped: Wrapped { | |
| switch self { | |
| case .some(let font): return font | |
| case .none: | |
| preconditionFailure(""" | |
| Could not load font from available fonts: | |
| \(UIFont.familyNames.joined(separator: ", ")) | |
| """) |
This file contains hidden or 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
| extension UILabel { | |
| static var scaledTitle: UILabel { | |
| let label = UILabel() | |
| label.adjustsFontForContentSizeCategory = true | |
| label.font = UIFont.jurassic.scaled | |
| label.numberOfLines = 0 | |
| return label | |
| } | |
| } |
This file contains hidden or 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
| @IBOutlet weak var outletLabel: UILabel! | |
| private lazy var titleLabel: UILabel = UILabel.scaledTitle | |
| private func setupLabels() { | |
| view.addSubview(titleLabel) | |
| outletLabel.font = UIFont.jurassicPark.bold.scaled(forTextStyle: .body, maxSize: 30.0) | |
| outletLabel.text = "Dinosaur" | |
| } |
OlderNewer