Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>

This commit is contained in:
Matt Bruce 2025-09-11 13:46:35 -05:00
parent 4ea684cbda
commit 6c5e0b4d74

View File

@ -12,32 +12,6 @@ import UIKit
/// Font sizing and typography utilities
struct FontUtils {
static func weightedFontName(
name: String,
weight: Font.Weight,
design: Font.Design
) -> String {
let weightSuffix = weight.uiFontWeightSuffix
switch design {
case .rounded:
if name.lowercased() == "system" { return "System" }
return name
+ (weightSuffix.isEmpty ? "-Rounded" : weightSuffix + "Rounded")
case .monospaced:
if name.lowercased() == "system" { return "Courier" }
return name == "Courier"
? name + weightSuffix
: name
+ (weightSuffix.isEmpty ? "-Mono" : weightSuffix + "Mono")
case .serif:
if name.lowercased() == "system" { return "TimesNewRomanPS" }
return name + weightSuffix
default:
return name + weightSuffix
}
}
static func calculateOptimalFontSize(
digit: String,
fontName: FontFamily,
@ -68,26 +42,24 @@ struct FontUtils {
return low
}
private static func tightBoundingBox(
for text: String,
withFont font: UIFont
) -> CGSize {
let attributedString = NSAttributedString(
string: text,
attributes: [.font: font]
)
let rect = attributedString.boundingRect(
with: CGSize(
width: CGFloat.greatestFiniteMagnitude,
height: CGFloat.greatestFiniteMagnitude
),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil
)
return CGSize(width: ceil(rect.width), height: ceil(rect.height))
static func createFont(
name: FontFamily,
weight: Font.Weight,
design: Font.Design,
size: CGFloat
) -> Font {
if name == .system {
return .system(size: size, weight: weight, design: design)
} else {
return .custom(weightedFontName(
name: name.rawValue,
weight: weight,
design: design
), size: size)
}
}
static func createUIFont(
private static func createUIFont(
name: FontFamily,
weight: Font.Weight,
design: Font.Design,
@ -119,20 +91,49 @@ struct FontUtils {
)
}
static func createFont(
name: FontFamily,
private static func tightBoundingBox(
for text: String,
withFont font: UIFont
) -> CGSize {
let attributedString = NSAttributedString(
string: text,
attributes: [.font: font]
)
let rect = attributedString.boundingRect(
with: CGSize(
width: CGFloat.greatestFiniteMagnitude,
height: CGFloat.greatestFiniteMagnitude
),
options: [.usesLineFragmentOrigin, .usesFontLeading],
context: nil
)
return CGSize(width: ceil(rect.width), height: ceil(rect.height))
}
private static func weightedFontName(
name: String,
weight: Font.Weight,
design: Font.Design,
size: CGFloat
) -> Font {
if name == .system {
return .system(size: size, weight: weight, design: design)
} else {
return .custom(weightedFontName(
name: name.rawValue,
weight: weight,
design: design
), size: size)
design: Font.Design
) -> String {
let weightSuffix = weight.uiFontWeightSuffix
switch design {
case .rounded:
if name.lowercased() == "system" { return "System" }
return name
+ (weightSuffix.isEmpty ? "-Rounded" : weightSuffix + "Rounded")
case .monospaced:
if name.lowercased() == "system" { return "Courier" }
return name == "Courier"
? name + weightSuffix
: name
+ (weightSuffix.isEmpty ? "-Mono" : weightSuffix + "Mono")
case .serif:
if name.lowercased() == "system" { return "TimesNewRomanPS" }
return name + weightSuffix
default:
return name + weightSuffix
}
}
}