Files
sodalive-ios/SodaLive/Sources/Extensions/FontModifier.swift
2026-05-15 19:50:30 +09:00

137 lines
3.5 KiB
Swift

//
// FontModifier.swift
// SodaLive
//
// Created by klaus on 1/23/26.
//
import SwiftUI
enum SodaTypography {
case heading1
case heading2
case heading3
case heading4
case body1
case body2
case body3
case body4
case body5
case body6
case caption1
case caption2
case caption3
var size: CGFloat {
switch self {
case .heading1:
return 24
case .heading2:
return 22
case .heading3:
return 20
case .heading4:
return 18
case .body1, .body2, .body3:
return 16
case .body4, .body5, .body6:
return 14
case .caption1, .caption2, .caption3:
return 12
}
}
var weight: SwiftUI.Font.Weight {
switch self {
case .heading1, .heading2, .heading3, .heading4, .body1, .body4, .caption1:
return .bold
case .body2, .body5, .caption2:
return .medium
case .body3, .body6, .caption3:
return .regular
}
}
}
private func resolvedLanguageCode(currentLocale: Locale) -> String? {
if let raw = UserDefaults.standard.string(forKey: "app.language"),
let option = LanguageOption(rawValue: raw),
option != .system {
return option.rawValue
}
return currentLocale.language.languageCode?.identifier
}
private func koreanFontName(for weight: SwiftUI.Font.Weight) -> String {
switch weight {
case .bold:
return "Pretendard-Bold"
case .medium:
return "Pretendard-Medium"
case .light:
return "Pretendard-Light"
default:
return "Pretendard-Regular"
}
}
private func japaneseFontName(for weight: SwiftUI.Font.Weight) -> String {
switch weight {
case .bold:
return "NotoSansJP-Bold"
case .medium:
return "NotoSansJP-Medium"
case .light:
return "NotoSansJP-Light"
default:
return "NotoSansJP-Regular"
}
}
struct FontModifier: ViewModifier {
@Environment(\.locale) private var locale
let size: CGFloat
let weight: SwiftUI.Font.Weight
func body(content: Content) -> some View {
switch resolvedLanguageCode(currentLocale: locale) {
case "ko":
content.font(.custom(koreanFontName(for: weight), size: size))
case "ja":
content.font(.custom(japaneseFontName(for: weight), size: size))
default:
content.font(.system(size: size, weight: weight))
}
}
}
extension View {
func appFont(size: CGFloat, weight: SwiftUI.Font.Weight = .regular) -> some View {
self.modifier(FontModifier(size: size, weight: weight))
}
func appFont(_ typography: SodaTypography) -> some View {
appFont(size: typography.size, weight: typography.weight)
}
}
extension Text {
func appFont(size: CGFloat, weight: SwiftUI.Font.Weight = .regular) -> Text {
if resolvedLanguageCode(currentLocale: .autoupdatingCurrent) == "ko" {
return self.font(.custom(koreanFontName(for: weight), size: size))
}
if resolvedLanguageCode(currentLocale: .autoupdatingCurrent) == "ja" {
return self.font(.custom(japaneseFontName(for: weight), size: size))
}
return self.font(.system(size: size, weight: weight))
}
func appFont(_ typography: SodaTypography) -> Text {
appFont(size: typography.size, weight: typography.weight)
}
}