Skip to content

Instantly share code, notes, and snippets.

struct ButtonStateStyle {
let foreground: UIColor
let background: UIImage
}
struct ButtonStyle {
let font: UIFont
let insets: UIEdgeInsets
let enabled: ButtonStateStyle
class WeightAdheringLabel: UILabel {
private var originalFont: UIFont?
override var font: UIFont! {
set {
self.originalFont = newValue
if traitCollection.legibilityWeight == .bold {
super.font = newValue.fontDescriptor.withSymbolicTraits(.traitBold).map { UIFont(descriptor: $0, size: newValue.pointSize) } ?? newValue
} else {
super.font = newValue
let font = UIFont(name: "Cochin", size: 16)!
let scaledFont = UIFontMetrics(forTextStyle: .body).scaledFont(for: font)
label.font = scaledFont
label.adjustsFontForContentSizeCategory = true
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
stack.axis = traitCollection.preferredContentSizeCategory > .extraExtraExtraLarge ? .vertical : .horizontal
}
stackView.axis = traitCollection.preferredContentSizeCategory > .extraExtraExtraLarge ? .vertical : .horizontal
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .caption1)
label.adjustsFontForContentSizeCategory = true
timeLabel.font = FontStyles.bigTime
timeLabel.adjustsFontForContentSizeCategory = false
struct FontStyles {
private static let customFont = UIFont(name: "Cochin", size: 15)!
static let headline = customFont.with(style: .headline, basePointSize: 34)
static let body = customFont.with(style: .body, basePointSize: UIFont.labelFontSize)
static let bigTime = customFont.withSize(66)
static let buttonPrimary = customFont.with(style: .body, basePointSize: UIFont.buttonFontSize)
static let buttonSecondary = customFont.with(style: .caption1, basePointSize: UIFont.smallSystemFontSize)
}
extension UIFont {
func with(style: UIFont.TextStyle, basePointSize: CGFloat, maxPointSize: CGFloat? = nil) -> UIFont {
if let maxPointSize = maxPointSize {
return UIFontMetrics(forTextStyle: style).scaledFont(for: self.withSize(basePointSize),
maximumPointSize: maxPointSize)
}
return UIFontMetrics(forTextStyle: style).scaledFont(for: self.withSize(basePointSize))
}
}
@krummler
krummler / article-button-styling-code1.swift
Last active April 9, 2020 10:12
Article - Simple button styling
button.backgroundColor = UIColor(named: "buttonBackground")
button.layer.cornerRadius = 8
button.layer.shadowColor = UIColor(named: "buttonShadow")?.cgColor
button.layer.shadowOpacity = 0.8
button.layer.shadowOffset = CGSize(width: 1, height: 1)
button.layer.borderWidth = 2
button.layer.borderColor = UIColor(named: "buttonBorder")?.cgColor