IceCubesApp/Packages/DesignSystem/Sources/DesignSystem/Font.swift

122 lines
3.5 KiB
Swift
Raw Normal View History

2023-01-17 20:08:05 +00:00
import Env
2023-01-22 05:38:30 +00:00
import SwiftUI
2023-01-17 20:08:05 +00:00
@MainActor
2023-01-22 05:38:30 +00:00
public extension Font {
// See https://gist.github.com/zacwest/916d31da5d03405809c4 for iOS values
// Custom values for Mac
private static let title = 28.0
private static let headline = onMac ? 20.0 : 17.0
private static let body = onMac ? 19.0 : 17.0
private static let callout = onMac ? 17.0 : 16.0
private static let subheadline = onMac ? 16.0 : 15.0
private static let footnote = onMac ? 15.0 : 13.0
private static let caption = onMac ? 14.0 : 12.0
2023-10-23 17:12:25 +00:00
private static let onMac = ProcessInfo.processInfo.isMacCatalystApp
2023-01-30 06:27:06 +00:00
private static func customFont(size: CGFloat, relativeTo textStyle: TextStyle) -> Font {
if let chosenFont = Theme.shared.chosenFont {
if chosenFont.fontName == ".AppleSystemUIFontRounded-Regular" {
return .system(size: size, design: .rounded)
2023-02-21 06:23:42 +00:00
} else {
return .custom(chosenFont.fontName, size: size, relativeTo: textStyle)
}
}
2023-01-30 06:27:06 +00:00
return .system(size: size, design: .default)
2023-01-17 20:08:05 +00:00
}
2023-01-30 06:27:06 +00:00
private static func customUIFont(size: CGFloat) -> UIFont {
if let chosenFont = Theme.shared.chosenFont {
return chosenFont.withSize(size)
}
return .systemFont(ofSize: size)
}
2023-01-30 06:27:06 +00:00
private static func userScaledFontSize(baseSize: CGFloat) -> CGFloat {
UIFontMetrics.default.scaledValue(for: baseSize * Theme.shared.fontSizeScale)
}
2023-01-30 06:27:06 +00:00
static var scaledTitle: Font {
customFont(size: userScaledFontSize(baseSize: title), relativeTo: .title)
}
2023-01-30 06:27:06 +00:00
static var scaledHeadline: Font {
customFont(size: userScaledFontSize(baseSize: headline), relativeTo: .headline).weight(.semibold)
}
2023-03-13 12:38:28 +00:00
static var scaledHeadlineFont: UIFont {
customUIFont(size: userScaledFontSize(baseSize: headline))
2023-02-22 21:13:46 +00:00
}
2023-03-13 12:38:28 +00:00
2023-03-09 19:27:03 +00:00
static var scaledBodyFocused: Font {
customFont(size: userScaledFontSize(baseSize: body + 2), relativeTo: .body)
}
2023-03-13 12:38:28 +00:00
2023-03-09 19:27:03 +00:00
static var scaledBodyFocusedFont: UIFont {
customUIFont(size: userScaledFontSize(baseSize: body + 2))
}
2023-01-30 06:27:06 +00:00
2023-01-22 05:38:30 +00:00
static var scaledBody: Font {
customFont(size: userScaledFontSize(baseSize: body), relativeTo: .body)
}
2023-02-26 05:45:57 +00:00
static var scaledBodyFont: UIFont {
customUIFont(size: userScaledFontSize(baseSize: body))
2023-02-22 21:13:46 +00:00
}
2023-01-30 06:27:06 +00:00
2023-01-22 05:38:30 +00:00
static var scaledBodyUIFont: UIFont {
customUIFont(size: userScaledFontSize(baseSize: body))
2023-01-18 07:04:52 +00:00
}
2023-01-30 06:27:06 +00:00
2023-01-22 05:38:30 +00:00
static var scaledCallout: Font {
customFont(size: userScaledFontSize(baseSize: callout), relativeTo: .callout)
}
2023-02-26 05:45:57 +00:00
static var scaledCalloutFont: UIFont {
customUIFont(size: userScaledFontSize(baseSize: body))
2023-02-22 21:13:46 +00:00
}
2023-01-30 06:27:06 +00:00
2023-01-22 05:38:30 +00:00
static var scaledSubheadline: Font {
customFont(size: userScaledFontSize(baseSize: subheadline), relativeTo: .subheadline)
}
2023-02-26 05:45:57 +00:00
static var scaledSubheadlineFont: UIFont {
customUIFont(size: userScaledFontSize(baseSize: subheadline))
2023-02-22 21:13:46 +00:00
}
2023-02-26 05:45:57 +00:00
2023-01-22 05:38:30 +00:00
static var scaledFootnote: Font {
customFont(size: userScaledFontSize(baseSize: footnote), relativeTo: .footnote)
}
2023-02-26 05:45:57 +00:00
static var scaledFootnoteFont: UIFont {
customUIFont(size: userScaledFontSize(baseSize: footnote))
2023-02-22 21:13:46 +00:00
}
2023-01-30 06:27:06 +00:00
2023-01-22 05:38:30 +00:00
static var scaledCaption: Font {
customFont(size: userScaledFontSize(baseSize: caption), relativeTo: .caption)
}
2023-02-26 05:45:57 +00:00
static var scaledCaptionFont: UIFont {
customUIFont(size: userScaledFontSize(baseSize: caption))
2023-02-22 21:13:46 +00:00
}
}
2023-02-21 06:23:42 +00:00
public extension UIFont {
func rounded() -> UIFont {
guard let descriptor = fontDescriptor.withDesign(.rounded) else {
return self
}
2023-02-21 06:23:42 +00:00
return UIFont(descriptor: descriptor, size: pointSize)
}
2023-03-13 12:38:28 +00:00
var emojiSize: CGFloat {
2023-03-13 12:38:28 +00:00
pointSize
}
2023-03-13 12:38:28 +00:00
var emojiBaselineOffset: CGFloat {
// Center emoji with capital letter size of font
2023-03-13 12:38:28 +00:00
-(emojiSize - capHeight) / 2
}
}