Skip to content

Instantly share code, notes, and snippets.

@gaussbeam
Last active July 13, 2023 08:15
Show Gist options
  • Save gaussbeam/903eb84041681a616f2307dbaa45844e to your computer and use it in GitHub Desktop.
Save gaussbeam/903eb84041681a616f2307dbaa45844e to your computer and use it in GitHub Desktop.
import SwiftUI
enum FontStyle: String, CaseIterable {
case titleExtraLarge
case titleMiddle
case body
case bodyBold
case description
case descriptionBold
}
// MARK: - Properties for SwiftUI
extension FontStyle {
var dynamicType: SwiftUI.Font {
switch self {
case .titleExtraLarge:
return .largeTitle
case .titleMiddle:
return .title2
case .body, .bodyBold:
return .body
case .description, .descriptionBold:
return .subheadline
}
}
var weight: SwiftUI.Font.Weight {
switch self {
case .titleExtraLarge, .titleMiddle:
return .bold
case .bodyBold, .descriptionBold:
return .bold
case .body, .description:
return .regular
}
}
/// 文字サイズ設定がデフォルト(.large)の場合に表示されるポイントサイズ(の目算値)
var estimatedDefaultPointSize: CGFloat {
switch self {
case .titleExtraLarge:
return 34
case .titleMiddle:
return 22.5
case .body, .bodyBold:
return 17
case .description, .descriptionBold:
return 15
}
}
}
// Mark: - Apply font style to Text
// iOS16.0+であれば`View.fontWeight()`が使えるので、`View`のextensionにできる
extension Text {
func fontStyle(_ style: FontStyle) -> some View {
self.font(style.dynamicType).fontWeight(style.weight)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment