Skip to content

Commit

Permalink
[WEAV-32] Font LineHeight 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
jisu15-kim committed Sep 14, 2024
1 parent d7eefaf commit 2e36c84
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 34 deletions.
12 changes: 1 addition & 11 deletions Projects/App/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ public struct ContentView: View {
public init() {}

public var body: some View {
VStack {
VStack(spacing: 20) {
Text("Hello, World!")
.padding()
.font(.pretendard(._300, size: 24))
.pretendard(weight: ._300, size: 20)
.robotoSlab(size: 12)

Expand All @@ -21,15 +20,6 @@ public struct ContentView: View {
SampleComponent()
.foregroundStyle(Color.red)
}
.onAppear {
Task {
do {
try await AuthEndpoint.requestSMSVerification(phone: "010-4602-2274")
} catch {
print(error)
}
}
}
}
}

Expand Down
21 changes: 17 additions & 4 deletions Projects/DesignSystem/DesignCore/Sources/Font+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,22 @@ import SwiftUI
import UIKit

public extension Font {
init(uiFont: UIFont) {
self = Font(uiFont as CTFont)
}
init(uiFont: UIFont) {
self = Font(uiFont as CTFont)
}
}


extension DesignCoreFontConvertible {
public func uiFont(size: CGFloat) -> UIFont {
guard let uiFont = UIFont(font: self, size: size) else {
fatalError("Unable to initialize font name: '\(name)', family: \(family)")
}
return uiFont
}
public func font(size: CGFloat) -> Font {
guard let font = UIFont(font: self, size: size) else {
fatalError("Unable to initialize font name: '\(name)', family: \(family)")
}
return Font(uiFont: font)
}
}
45 changes: 36 additions & 9 deletions Projects/DesignSystem/DesignCore/Sources/Pretendard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@ public enum PretendardWeight {
}
}

public extension Font {
static func pretendard(_ weight: PretendardWeight, size: CGFloat) -> Font {
return weight.font.font(size: size)
}
}
//public extension Font {
// static func pretendard(_ weight: PretendardWeight, size: CGFloat) -> Font {
// return weight.font.font(size: size)
// }
//
// static func pretendard(
// _ weight: PretendardWeight,
// size: CGFloat,
// lineHeight: CGFloat
// ) -> Font {
// return weight.font.font(size: size)
// }
//}

public extension UIFont {
static func pretendard(_ weight: PretendardWeight, size: CGFloat) -> UIFont {
Expand All @@ -59,15 +67,34 @@ public extension UIFont {
private struct PretendardModifier: ViewModifier {
let weight: PretendardWeight
let size: CGFloat
var lineHeight: CGFloat? = nil

func body(content: Content) -> some View {
content
.font(.pretendard(weight, size: size))
if let lineHeight {
let uifont = weight.font.uiFont(size: size)
content
.font(Font(uiFont: uifont))
.lineSpacing(lineHeight - uifont.lineHeight)
.padding(.vertical, (lineHeight - uifont.lineHeight))
} else {
content
.font(Font(uiFont: weight.font.uiFont(size: size)))
}
}
}

public extension View {
func pretendard(weight: PretendardWeight, size: CGFloat) -> some View {
return modifier(PretendardModifier(weight: weight, size: size))
func pretendard(
weight: PretendardWeight,
size: CGFloat,
lineHeight: CGFloat? = nil
) -> some View {
return modifier(
PretendardModifier(
weight: weight,
size: size,
lineHeight: lineHeight
)
)
}
}
25 changes: 21 additions & 4 deletions Projects/DesignSystem/DesignCore/Sources/RobotoSlab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,32 @@ public extension UIFont {

private struct RobotoSlabModifier: ViewModifier {
let size: CGFloat
var lineHeight: CGFloat? = nil

func body(content: Content) -> some View {
content
.font(.robotoSlab(size: size))
if let lineHeight {
let uifont = UIFont.robotoSlab(size: size)
content
.font(Font(uiFont: uifont))
.lineSpacing(lineHeight - uifont.lineHeight)
.padding(.vertical, (lineHeight - uifont.lineHeight))
} else {
content
.font(.robotoSlab(size: size))
}
}
}

public extension View {
func robotoSlab(size: CGFloat) -> some View {
return modifier(RobotoSlabModifier(size: size))
func robotoSlab(
size: CGFloat,
lineHeight: CGFloat? = nil
) -> some View {
return modifier(
RobotoSlabModifier(
size: size,
lineHeight: lineHeight
)
)
}
}
7 changes: 1 addition & 6 deletions Tuist/ResourceSynthesizers/Fonts.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ import SwiftUI
{{accessModifier}} let name: String
{{accessModifier}} let family: String
{{accessModifier}} let path: String
{{accessModifier}} func font(size: CGFloat) -> Font {
guard let font = UIFont(font: self, size: size) else {
fatalError("Unable to initialize font name: '\(name)', family: \(family)")
}
return Font(uiFont: font)
}

{{accessModifier}} func register() {
// swiftlint:disable:next conditional_returns_on_newline
guard let url = url else { return }
Expand Down

0 comments on commit 2e36c84

Please sign in to comment.