Skip to content

Instantly share code, notes, and snippets.

@keehun
Last active August 19, 2018 00:32
Show Gist options
  • Save keehun/af0a7bdacddc0788f2478ba79e81b3ec to your computer and use it in GitHub Desktop.
Save keehun/af0a7bdacddc0788f2478ba79e81b3ec to your computer and use it in GitHub Desktop.
Practical Dynamic Type, Part 2
import Foundation
import UIKit
// MARK: - Hal9000
class Hal9000: UIView {
/// A `UIFont` that will be used as the typeface of Hal9000's messages
let staticFont: UIFont = UIFont(name: "GillSans-Light", size: 36.0)!
/// A `UILabel` that will render Hal9000's messages
lazy var dialog: UILabel = {
let label = UILabel()
/// FontMetrics will make that font scale based on system settings
label.font = FontMetrics().scaledFont(for: staticFont)
label.text = "I'm sorry, Dave. I'm afraid I can't do that."
label.adjustsFontForContentSizeCategory = true
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
/// Add `dialog` to Hal9000's view
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),
])
}
/// Although "required," we don't have to implement init(coder:) for now
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