Skip to content

Instantly share code, notes, and snippets.

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

Felipe Marino marinofelipe

👨‍💻
Focusing
View GitHub Profile
- name: Swift by Sundell
rss_url: https://swiftbysundell.com/rss
logo_url: https://www.google.com/s2/favicons?sz=256&domain_url=www.swiftbysundell.com
- name: Swift Wings
rss_url: https://www.onswiftwings.com/index.xml
- name: Swift Rocks
rss_url: https://swiftrocks.com/rss.xml
@marinofelipe
marinofelipe / UILabel.swift
Last active March 13, 2019 04:14
Blog post / Dynamic Type - Setting a custom scalable font with max size to a label
label.font = UIFont.jurassicPark.regular.sized(40.0).scaled(forTextStyle: .body, maxSize: 80.0)
@marinofelipe
marinofelipe / UILabel.swift
Last active March 13, 2019 04:13
Blog post / Dynamic Type - Passing a custom font to label's font
label.font = UIFont.jurassicPark.semibold.sized(30.0)
@marinofelipe
marinofelipe / CustomFont.swift
Last active March 13, 2019 04:12
Blog post / Dynamic Type - Custom font protocol extended for enums that has RawValue of type String
protocol CustomFont {
func sized(_ size: CGFloat) -> UIFont?
}
extension CustomFont where Self: RawRepresentable, Self.RawValue == String {
func sized(_ size: CGFloat) -> UIFont? {
return UIFont(name: self.rawValue, size: size)
}
}
@marinofelipe
marinofelipe / CustomFont.swift
Last active April 8, 2019 03:24
Blog post / Dynamic Type - Custom font improved to work with unwrapped font
extension CustomFont where Self: RawRepresentable, Self.RawValue == String {
func sized(_ size: CGFloat) -> UIFont {
return UIFont(name: self.rawValue, size: size).unwrapped
}
}
@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"
}
@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 / 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 / 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 / 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)
}