Skip to content

Commit

Permalink
Add size specifiable preferredFont method
Browse files Browse the repository at this point in the history
  • Loading branch information
woin2ee committed Apr 27, 2024
1 parent 50b3623 commit 48bf32c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/UIKitPlus/Extensions/UIFont+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ extension UIFont {
return style.metrics.scaledFont(for: font)
}
}

/// Returns an instance of the system font for the specified `size`(in points) and `weight` with scaling for the user's selected content size category.
/// - Parameters:
/// - size: The size (in points) to which the font is scaled. This value must be greater than 0.0.
/// - weight: The weight of the font, specified as a font weight constant. For a list of possible values, see [UIFont.Weight](https://developer.apple.com/documentation/uikit/uifont/weight).
/// - maximumPointSize: The maximum point size allowed for the font. Use this value to constrain the font to the specified size when your interface cannot accommodate text that is any larger.
/// - Important: If you set the `size` to less than 13, it will be difficult for users to read.
/// - Returns: The system font associated with the specified `size` and `weight`.
public static func preferredFont(ofSize size: CGFloat, weight: UIFont.Weight = .regular, maximumPointSize: CGFloat? = nil) -> UIFont {
let font: UIFont = .systemFont(ofSize: size, weight: weight)

if let maximumPointSize = maximumPointSize {
return UIFontMetrics.default.scaledFont(for: font, maximumPointSize: maximumPointSize)
} else {
return UIFontMetrics.default.scaledFont(for: font)
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ final class UIFontPreferredFontExampleViewController: UIViewController {
return label
}()

lazy var sizedPreferredFontLabel: UILabel = {
let label = UILabel()
label.text = "Lorem ipsum dolor sit amet."
label.adjustsFontForContentSizeCategory = true
label.font = .preferredFont(ofSize: 20, weight: .ultraLight)
return label
}()

override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -28,6 +36,8 @@ final class UIFontPreferredFontExampleViewController: UIViewController {
titleLabel.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(bodyLabel)
bodyLabel.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(sizedPreferredFontLabel)
sizedPreferredFontLabel.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 10),
Expand All @@ -37,6 +47,10 @@ final class UIFontPreferredFontExampleViewController: UIViewController {
bodyLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10),
bodyLabel.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 10),
bodyLabel.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -10),

sizedPreferredFontLabel.topAnchor.constraint(equalTo: bodyLabel.bottomAnchor, constant: 10),
sizedPreferredFontLabel.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 10),
sizedPreferredFontLabel.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -10),
])
}
}

0 comments on commit 48bf32c

Please sign in to comment.