Skip to content

Instantly share code, notes, and snippets.

View marinofelipe's full-sized avatar
👨‍💻
Focusing

Felipe Marino marinofelipe

👨‍💻
Focusing
View GitHub Profile
@marinofelipe
marinofelipe / ViewController.swift
Last active April 8, 2019 03:22
Blog post / Dynamic Type - Setting a dynamic size text to a label using one of system's built-in text styles
label.font = UIFont.preferedFont(forTextStyle: .body)
@marinofelipe
marinofelipe / ViewController.swift
Last active April 8, 2019 03:22
Blog post / Dynamic Type - Adapting the label to adjust for content size category changes
if #available(iOS 10.0, *) {
label.adjustsFontForContentSizeCategory = true
}
@marinofelipe
marinofelipe / ViewController.swift
Last active April 8, 2019 03:22
Blog post / Dynamic Type - Shows how to listen to content size category changes above and under iOS 10
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)
}
}
@marinofelipe
marinofelipe / ViewController.swift
Last active April 8, 2019 03:23
Blog post / Dynamic Type - Setting a scaled custom font using UIFontMetrics with title1 text style
if #available(iOS 11.0, *) {
if let font = UIFont(name: "custom", size: 20.0) {
label.font = UIFontMetrics(forTextStyle: .title1).scaledFont(for: font)
}
}
@marinofelipe
marinofelipe / CustomFont.swift
Last active April 8, 2019 03:23
Blog post / Dynamic Type - App's custom fonts enumeration
enum jurassicPark: String {
case
regular = "JurassicPark-Regular",
semibold = "JurassicPark-Semibold",
bold = "JurassicPark-Bold"
}
@marinofelipe
marinofelipe / ScalableFont.swift
Last active March 13, 2019 04:13
Blog post / Dynamic Type - Defines scalable fonts behaviour and extends itself when conditionally conforms to raw representable of raw type string (what we use in our custom fonts)
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)
}
@marinofelipe
marinofelipe / UIFont+CustomFont.swift
Last active March 13, 2019 04:13
Blog post / Dynamic Type - UIFont extension with jurassic park font that adopts CustomFont protocol
extension UIFont {
enum jurassicPark: String, CustomFont {
case
regular = "JurassicPark-Regular",
semibold = "JurassicPark-Semibold",
bold = "JurassicPark-Bold"
}
}
@marinofelipe
marinofelipe / UIFont+Optional.swift
Last active April 8, 2019 03:24
Blog post / Dynamic Type - Extending Optional when the wrapped value is of type UIFont, so we can return the custom font object or precondition failure
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: ", "))
""")
@marinofelipe
marinofelipe / UILabel+Factory.swift
Last active March 13, 2019 04:15
Blog post / Dynamic Type - Using static factory to generate a specific label that has custom scalable font and adjusts it for content size category
extension UILabel {
static var scaledTitle: UILabel {
let label = UILabel()
label.adjustsFontForContentSizeCategory = true
label.font = UIFont.jurassic.scaled
label.numberOfLines = 0
return label
}
}
@marinofelipe
marinofelipe / ViewController.swift
Last active March 13, 2019 03:52
Blog post / Dynamic Type - Final usage for custom fonts and auto updating views
@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"
}