Created
August 9, 2018 03:27
-
-
Save keehun/49296813bd0216d4d8b3921c729fab74 to your computer and use it in GitHub Desktop.
Practical Dynamic Type, Part 2
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
| import Foundation | |
| import UIKit | |
| class Hal9000: UIView { | |
| let font: UIFont = UIFont(name: "GillSans-Light", size: 36.0)! | |
| lazy var dialog: UILabel = { | |
| let label = UILabel() | |
| label.font = FontMetrics.default.scaledFont(for: self.font) | |
| label.text = "I'm sorry, Dave. I'm afraid I can't do that." | |
| label.translatesAutoresizingMaskIntoConstraints = false | |
| label.numberOfLines = 0 | |
| return label | |
| }() | |
| override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| addSubview(dialog) | |
| NSLayoutConstraint.activate([ | |
| dialog.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor), | |
| dialog.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor), | |
| dialog.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16), | |
| dialog.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16), | |
| ]) | |
| NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryChanged), | |
| name: Notification.Name.FontMetricsContentSizeCategoryDidChange, | |
| object: nil) | |
| } | |
| @objc func contentSizeCategoryChanged() { | |
| dialog.font = FontMetrics.default.scaledFont(for: self.font) | |
| } | |
| required init?(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) not implemented for this article.") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment