Files
sodalive-ios/SodaLive/Sources/Extensions/KoreanFontModifier.swift
2026-01-26 11:37:40 +09:00

83 lines
2.3 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 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 KoreanFontModifier: 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(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(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))
}
}