From 98c177d0e44395efb9a1690cac367a05a39d24e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=8B=A0=EB=AF=BC=ED=98=B8?= <68156836+alsh0807@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:40:44 +0900 Subject: [PATCH] feat: TextStyle --- .../SDS/Foundation/Typography/Font+Ext.swift | 28 ++++++ .../Typography/Pretendard+Ext.swift | 2 +- .../Foundation/Typography/SopoFont+Ext.swift | 17 ++-- .../SDS/Foundation/Typography/SopoFont.swift | 10 +- .../Typography/SopoTextStyle+Ext.swift | 93 +++++++++++++++++++ .../Foundation/Typography/SopoTextStyle.swift | 17 ++++ 6 files changed, 157 insertions(+), 10 deletions(-) create mode 100644 Sources/SDS/Foundation/Typography/SopoTextStyle+Ext.swift create mode 100644 Sources/SDS/Foundation/Typography/SopoTextStyle.swift diff --git a/Sources/SDS/Foundation/Typography/Font+Ext.swift b/Sources/SDS/Foundation/Typography/Font+Ext.swift index 70c26d1..ca9ff93 100644 --- a/Sources/SDS/Foundation/Typography/Font+Ext.swift +++ b/Sources/SDS/Foundation/Typography/Font+Ext.swift @@ -4,4 +4,32 @@ extension Font { public static func pretendard( _ weight: SopoFont.Pretendard = .regular, size: CGFloat ) -> Font { return weight.font(size: size) } + + public static func title( _ style: SopoTextStyle.Title1 ) -> Font { + return style.font + } + + public static func title2( _ style: SopoTextStyle.Title2 ) -> Font { + return style.font + } + + public static func headline( _ style: SopoTextStyle.Headline1 ) -> Font { + return style.font + } + + public static func headline2( _ style: SopoTextStyle.Headline2 ) -> Font { + return style.font + } + + public static func body( _ style: SopoTextStyle.Body ) -> Font { + return style.font + } + + public static func label( _ style: SopoTextStyle.Label ) -> Font { + return style.font + } + + public static func caption( _ style: SopoTextStyle.Caption ) -> Font { + return style.font + } } diff --git a/Sources/SDS/Foundation/Typography/Pretendard+Ext.swift b/Sources/SDS/Foundation/Typography/Pretendard+Ext.swift index edee833..11c3dad 100644 --- a/Sources/SDS/Foundation/Typography/Pretendard+Ext.swift +++ b/Sources/SDS/Foundation/Typography/Pretendard+Ext.swift @@ -1,6 +1,6 @@ import SwiftUI -extension SopoFont.Pretendard: SopoFont.Fontable { +extension SopoFont.Pretendard: SopoFont.CanDefine { public var name: String { switch self { case .thin: diff --git a/Sources/SDS/Foundation/Typography/SopoFont+Ext.swift b/Sources/SDS/Foundation/Typography/SopoFont+Ext.swift index 22a4b4e..a408c98 100644 --- a/Sources/SDS/Foundation/Typography/SopoFont+Ext.swift +++ b/Sources/SDS/Foundation/Typography/SopoFont+Ext.swift @@ -1,18 +1,20 @@ import SwiftUI extension SopoFont { - public protocol Fontable { + public protocol CanDefine { var name: String { get } } + + public protocol Fontable { + var font: Font { get } + } } -enum SopoFontError: Error { - case fileNotFound -} -extension SopoFont.Fontable { - private func loadFont() { + +extension SopoFont.CanDefine { + public func loadFont() { guard let asset = NSDataAsset(name: self.name, bundle: .module), let provider = CGDataProvider(data: asset.data as NSData), let font = CGFont(provider), @@ -22,7 +24,6 @@ extension SopoFont.Fontable { } public func font( size: CGFloat ) -> Font { - self.loadFont() - return Font.custom(name, size: size) + return Font.custom(self.name, size: size) } } diff --git a/Sources/SDS/Foundation/Typography/SopoFont.swift b/Sources/SDS/Foundation/Typography/SopoFont.swift index f9c6057..3bfaccf 100644 --- a/Sources/SDS/Foundation/Typography/SopoFont.swift +++ b/Sources/SDS/Foundation/Typography/SopoFont.swift @@ -1,5 +1,13 @@ import SwiftUI public struct SopoFont { - public enum Pretendard { case thin, extraLight, light, regular, medium, semibold, bold, extraBold, black } + init() { + Pretendard.allCases.forEach { item in + item.loadFont() + } + } + + public enum Pretendard: CaseIterable { case thin, extraLight, light, regular, medium, semibold, bold, extraBold, black } + + } diff --git a/Sources/SDS/Foundation/Typography/SopoTextStyle+Ext.swift b/Sources/SDS/Foundation/Typography/SopoTextStyle+Ext.swift new file mode 100644 index 0000000..8ccd754 --- /dev/null +++ b/Sources/SDS/Foundation/Typography/SopoTextStyle+Ext.swift @@ -0,0 +1,93 @@ +import SwiftUI + +extension SopoTextStyle.Title1: SopoFont.Fontable { + public var font: Font { + switch self { + case .bold: + .pretendard(.extraBold, size: 32) + case .medium: + .pretendard(.medium, size: 32) + case .regular: + .pretendard(.regular, size: 32) + } + } +} + +extension SopoTextStyle.Title2: SopoFont.Fontable { + public var font: Font { + switch self { + case .bold: + .pretendard(.bold, size: 28) + case .medium: + .pretendard(.medium, size: 28) + case .regular: + .pretendard(.regular, size: 28) + } + } +} + +extension SopoTextStyle.Headline1: SopoFont.Fontable { + public var font: Font { + switch self { + case .bold: + .pretendard(.bold, size: 24) + case .medium: + .pretendard(.medium, size: 24) + case .regular: + .pretendard(.regular, size: 24) + } + } +} + +extension SopoTextStyle.Headline2: SopoFont.Fontable { + public var font: Font { + switch self { + case .bold: + .pretendard(.bold, size: 20) + case .medium: + .pretendard(.medium, size: 20) + case .regular: + .pretendard(.regular, size: 20) + } + } +} + +extension SopoTextStyle.Body: SopoFont.Fontable { + public var font: Font { + switch self { + case .bold: + .pretendard(.bold, size: 16) + case .medium: + .pretendard(.medium, size: 16) + case .regular: + .pretendard(.regular, size: 16) + } + } +} + + +extension SopoTextStyle.Label: SopoFont.Fontable { + public var font: Font { + switch self { + case .bold: + .pretendard(.bold, size: 14) + case .medium: + .pretendard(.medium, size: 14) + case .regular: + .pretendard(.regular, size: 14) + } + } +} + +extension SopoTextStyle.Caption: SopoFont.Fontable { + public var font: Font { + switch self { + case .bold: + .pretendard(.bold, size: 12) + case .medium: + .pretendard(.medium, size: 12) + case .regular: + .pretendard(.regular, size: 12) + } + } +} diff --git a/Sources/SDS/Foundation/Typography/SopoTextStyle.swift b/Sources/SDS/Foundation/Typography/SopoTextStyle.swift new file mode 100644 index 0000000..83c0260 --- /dev/null +++ b/Sources/SDS/Foundation/Typography/SopoTextStyle.swift @@ -0,0 +1,17 @@ +import SwiftUI + +public struct SopoTextStyle { + + public enum Title1 { case bold, medium, regular } + public enum Title2 { case bold, medium, regular } + + public enum Headline1 { case bold, medium, regular } + public enum Headline2 { case bold, medium, regular } + + public enum Body { case bold, medium, regular } + + public enum Label { case bold, medium, regular } + + public enum Caption { case bold, medium, regular } + +}