Skip to content

Commit

Permalink
Textに.font(charcoalTypography:)と.foregroundStyle(charcoalColor:)を足しました
Browse files Browse the repository at this point in the history
  • Loading branch information
natmark committed Oct 25, 2024
1 parent 5eb0378 commit 6dd9614
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ struct TypographiesView: View {
Text(Const.numberText)
.charcoalTypography20RegularMono()
}
VStack(alignment: .leading, spacing: 4) {
Text("20 Attributed String")

Text("Atributed")
.font(charcoalTypography: .bold20)
+
Text("String")
.font(charcoalTypography: .regular20)
}
}
Group {
VStack(alignment: .leading, spacing: 4) {
Expand Down Expand Up @@ -74,6 +83,15 @@ struct TypographiesView: View {
Text(Const.numberText)
.charcoalTypography16RegularMono()
}
VStack(alignment: .leading, spacing: 4) {
Text("16 Attributed String")

Text("Atributed")
.font(charcoalTypography: .bold16)
+
Text("String")
.font(charcoalTypography: .regular16)
}
}
Group {
VStack(alignment: .leading, spacing: 4) {
Expand Down Expand Up @@ -106,6 +124,15 @@ struct TypographiesView: View {
Text(Const.numberText)
.charcoalTypography14RegularMono()
}
VStack(alignment: .leading, spacing: 4) {
Text("14 Attributed String")

Text("Atributed")
.font(charcoalTypography: .bold14)
+
Text("String")
.font(charcoalTypography: .regular14)
}
}
Group {
VStack(alignment: .leading, spacing: 4) {
Expand Down Expand Up @@ -138,6 +165,15 @@ struct TypographiesView: View {
Text(Const.numberText)
.charcoalTypography12RegularMono()
}
VStack(alignment: .leading, spacing: 4) {
Text("12 Attributed String")

Text("Atributed")
.font(charcoalTypography: .bold12)
+
Text("String")
.font(charcoalTypography: .regular12)
}
}
Group {
VStack(alignment: .leading, spacing: 4) {
Expand Down Expand Up @@ -170,6 +206,15 @@ struct TypographiesView: View {
Text(Const.numberText)
.charcoalTypography10RegularMono()
}
VStack(alignment: .leading, spacing: 4) {
Text("10 Attributed String")

Text("Atributed")
.font(charcoalTypography: .bold10)
+
Text("String")
.font(charcoalTypography: .regular10)
}
}
}
.frame(maxWidth: .infinity)
Expand Down
12 changes: 12 additions & 0 deletions Sources/CharcoalSwiftUI/Backport/View/ForegroundStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ extension Backport where Content: View {
}
}
}

extension Backport where Content == Text {
func foregroundStyle(_ color: Color) -> Text {
if #available(iOS 17, *) {
content
.foregroundStyle(color)
} else {
content
.foregroundColor(color)
}
}
}
14 changes: 13 additions & 1 deletion Sources/CharcoalSwiftUI/Colors/Foregrounds.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,21 @@ public extension View {
}
}

public extension Text {
func foregroundStyle(charcoalColor: CharcoalAsset.ColorPaletteGenerated) -> Text {
self.backport.foregroundStyle(charcoalColor.colorAsset.swiftUIColor)
}
}

#Preview {
ZStack {
VStack {
Text("Charcoal")
.foregroundStyle(charcoalColor: .brand)

Text("Attributed")
.foregroundStyle(charcoalColor: .brand)
+
Text("String")
.foregroundStyle(charcoalColor: .text3)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import SwiftUI

public extension Text {
enum CharcoalTypography {
case regular10
case bold10
case regular12
case bold12
case regular14
case bold14
case regular16
case bold16
case regular20
case bold20

typealias Value = (size: CGFloat, weight: UIFont.Weight)

var value: Value {
switch self {
case .regular10:
return (size: 10, weight: .regular)
case .bold10:
return (size: 10, weight: .bold)
case .regular12:
return (size: CGFloat(charcoalFoundation.typography.size.the12.fontSize), weight: .regular)
case .bold12:
return (size: CGFloat(charcoalFoundation.typography.size.the12.fontSize), weight: .bold)
case .regular14:
return (size: CGFloat(charcoalFoundation.typography.size.the14.fontSize), weight: .regular)
case .bold14:
return (size: CGFloat(charcoalFoundation.typography.size.the14.fontSize), weight: .bold)
case .regular16:
return (size: CGFloat(charcoalFoundation.typography.size.the16.fontSize), weight: .regular)
case .bold16:
return (size: CGFloat(charcoalFoundation.typography.size.the16.fontSize), weight: .bold)
case .regular20:
return (size: CGFloat(charcoalFoundation.typography.size.the20.fontSize), weight: .regular)
case .bold20:
return (size: CGFloat(charcoalFoundation.typography.size.the20.fontSize), weight: .bold)
}
}
}

func font(charcoalTypography: CharcoalTypography) -> Text {
self
.font(
.custom(UIFont.monospacedSystemFont(ofSize: .zero, weight: charcoalTypography.value.weight).fontName,
size: charcoalTypography.value.size,
relativeTo: .body)
)
}
}

0 comments on commit 6dd9614

Please sign in to comment.