From 6c5e0b4d7485a0a9e9ab3fe3ff881cbe0068fb56 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 11 Sep 2025 13:46:35 -0500 Subject: [PATCH] Signed-off-by: Matt Bruce --- TheNoiseClock/Core/Utilities/FontUtils.swift | 115 ++++++++++--------- 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/TheNoiseClock/Core/Utilities/FontUtils.swift b/TheNoiseClock/Core/Utilities/FontUtils.swift index d51bede..c97aab8 100644 --- a/TheNoiseClock/Core/Utilities/FontUtils.swift +++ b/TheNoiseClock/Core/Utilities/FontUtils.swift @@ -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 } } + }