Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
parent
4ea684cbda
commit
6c5e0b4d74
@ -12,32 +12,6 @@ import UIKit
|
|||||||
/// Font sizing and typography utilities
|
/// Font sizing and typography utilities
|
||||||
struct FontUtils {
|
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(
|
static func calculateOptimalFontSize(
|
||||||
digit: String,
|
digit: String,
|
||||||
fontName: FontFamily,
|
fontName: FontFamily,
|
||||||
@ -68,26 +42,24 @@ struct FontUtils {
|
|||||||
return low
|
return low
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func tightBoundingBox(
|
static func createFont(
|
||||||
for text: String,
|
name: FontFamily,
|
||||||
withFont font: UIFont
|
weight: Font.Weight,
|
||||||
) -> CGSize {
|
design: Font.Design,
|
||||||
let attributedString = NSAttributedString(
|
size: CGFloat
|
||||||
string: text,
|
) -> Font {
|
||||||
attributes: [.font: font]
|
if name == .system {
|
||||||
)
|
return .system(size: size, weight: weight, design: design)
|
||||||
let rect = attributedString.boundingRect(
|
} else {
|
||||||
with: CGSize(
|
return .custom(weightedFontName(
|
||||||
width: CGFloat.greatestFiniteMagnitude,
|
name: name.rawValue,
|
||||||
height: CGFloat.greatestFiniteMagnitude
|
weight: weight,
|
||||||
),
|
design: design
|
||||||
options: [.usesLineFragmentOrigin, .usesFontLeading],
|
), size: size)
|
||||||
context: nil
|
}
|
||||||
)
|
|
||||||
return CGSize(width: ceil(rect.width), height: ceil(rect.height))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static func createUIFont(
|
private static func createUIFont(
|
||||||
name: FontFamily,
|
name: FontFamily,
|
||||||
weight: Font.Weight,
|
weight: Font.Weight,
|
||||||
design: Font.Design,
|
design: Font.Design,
|
||||||
@ -119,20 +91,49 @@ struct FontUtils {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
static func createFont(
|
private static func tightBoundingBox(
|
||||||
name: FontFamily,
|
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,
|
weight: Font.Weight,
|
||||||
design: Font.Design,
|
design: Font.Design
|
||||||
size: CGFloat
|
) -> String {
|
||||||
) -> Font {
|
let weightSuffix = weight.uiFontWeightSuffix
|
||||||
if name == .system {
|
|
||||||
return .system(size: size, weight: weight, design: design)
|
switch design {
|
||||||
} else {
|
case .rounded:
|
||||||
return .custom(weightedFontName(
|
if name.lowercased() == "system" { return "System" }
|
||||||
name: name.rawValue,
|
return name
|
||||||
weight: weight,
|
+ (weightSuffix.isEmpty ? "-Rounded" : weightSuffix + "Rounded")
|
||||||
design: design
|
case .monospaced:
|
||||||
), size: size)
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user