From 87e9876e25a0f05bd08e4e2b76f8103519d2c0f9 Mon Sep 17 00:00:00 2001 From: Jisu Kim Date: Sat, 14 Sep 2024 15:54:23 +0900 Subject: [PATCH] =?UTF-8?q?[WEAV-32]=20DesignSystem=20-=20Typography=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DesignCore/Sources/Pretendard.swift | 14 --- .../DesignCore/Sources/Typography.swift | 106 ++++++++++++++++++ 2 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 Projects/DesignSystem/DesignCore/Sources/Typography.swift diff --git a/Projects/DesignSystem/DesignCore/Sources/Pretendard.swift b/Projects/DesignSystem/DesignCore/Sources/Pretendard.swift index 48af49f..ae19a4d 100644 --- a/Projects/DesignSystem/DesignCore/Sources/Pretendard.swift +++ b/Projects/DesignSystem/DesignCore/Sources/Pretendard.swift @@ -44,20 +44,6 @@ public enum PretendardWeight { } } -//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 { return .init(font: weight.font, size: size)! diff --git a/Projects/DesignSystem/DesignCore/Sources/Typography.swift b/Projects/DesignSystem/DesignCore/Sources/Typography.swift new file mode 100644 index 0000000..7114b0e --- /dev/null +++ b/Projects/DesignSystem/DesignCore/Sources/Typography.swift @@ -0,0 +1,106 @@ +// +// Typography.swift +// DesignCore +// +// Created by 김지수 on 9/14/24. +// Copyright © 2024 com.studentcenter. All rights reserved. +// + +import SwiftUI + +public enum Typography { + // EN - Medium + case en_medium_20 + case en_medium_16 + + // Pretendard - semibold + case semibold_28 + case semibold_24 + case semibold_20 + case semibold_14 + + // Pretendard - medium + case medium_18 + case medium_16 + case medium_14 + + // Pretendard - regular + case regular_15 + case regular_14 + case regular_12 +} + +extension Typography { + var fontSize: CGFloat { + switch self { + case .en_medium_20: return 20 + case .en_medium_16: return 16 + case .semibold_28: return 28 + case .semibold_24: return 24 + case .semibold_20: return 20 + case .semibold_14: return 14 + case .medium_18: return 18 + case .medium_16: return 16 + case .medium_14: return 14 + case .regular_15: return 15 + case .regular_14: return 14 + case .regular_12: return 12 + } + } + + var lineHeight: CGFloat { + switch self { + case .en_medium_20: return 20 + case .en_medium_16: return 16 + default: return fontSize * 1.5 + } + } +} + +extension Typography { + var pretendardWeight: PretendardWeight? { + switch self { + case .en_medium_20, .en_medium_16: + return nil + + case .semibold_28, .semibold_24, .semibold_20, .semibold_14: + return ._600 + + case .medium_18, .medium_16, .medium_14: + return ._500 + + case .regular_15, .regular_14, .regular_12: + return ._400 + } + } +} + +private struct TypographyViewModifier: ViewModifier { + let typography: Typography + + func body(content: Content) -> some View { + switch typography { + case .en_medium_16, .en_medium_20: + content + .robotoSlab( + size: typography.fontSize, + lineHeight: typography.lineHeight + ) + default: + content + .pretendard( + weight: typography.pretendardWeight!, + size: typography.fontSize, + lineHeight: typography.lineHeight + ) + } + } +} + +public extension View { + func typography(_ typograpy: Typography) -> some View { + return modifier( + TypographyViewModifier(typography: typograpy) + ) + } +}