Files
sodalive-ios/SodaLive/Sources/Extensions/KoreanFontModifier.swift

60 lines
1.6 KiB
Swift

//
// KoreanFontModifier.swift
// SodaLive
//
// Created by klaus on 1/23/26.
//
import SwiftUI
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 appFontName(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"
}
}
struct KoreanFontModifier: ViewModifier {
@Environment(\.locale) private var locale
let size: CGFloat
let weight: SwiftUI.Font.Weight
func body(content: Content) -> some View {
if resolvedLanguageCode(currentLocale: locale) == "ko" {
content.font(.custom(appFontName(for: weight), size: size))
} else {
content.font(.system(size: size, weight: weight))
}
}
}
extension View {
func appFont(size: CGFloat, weight: SwiftUI.Font.Weight = .regular) -> some View {
self.modifier(KoreanFontModifier(size: size, weight: weight))
}
}
extension Text {
func appFont(size: CGFloat, weight: SwiftUI.Font.Weight = .regular) -> Text {
if resolvedLanguageCode(currentLocale: .autoupdatingCurrent) == "ko" {
return self.font(.custom(appFontName(for: weight), size: size))
}
return self.font(.system(size: size, weight: weight))
}
}