|
8 | 8 | import SwiftUI
|
9 | 9 | import YDS_Essential
|
10 | 10 |
|
11 |
| -public struct YDSLabel: UIViewRepresentable { |
12 |
| - let text: String? |
13 |
| - let typoStyle: UIFont? |
14 |
| - let textColor: Color? |
15 |
| - let maxWidth: CGFloat? |
16 |
| - let numberOfLines: Int? |
17 |
| - let lineBreakMode: NSLineBreakMode? |
18 |
| - let alignment: NSTextAlignment? |
19 |
| - let lineBreakStrategy: NSParagraphStyle.LineBreakStrategy? |
20 |
| - |
21 |
| - public enum LabelTypoStyle: CaseIterable { |
22 |
| - case display1 |
23 |
| - case display2 |
24 |
| - |
25 |
| - case title1 |
26 |
| - case title2 |
27 |
| - case title3 |
28 |
| - |
29 |
| - case subtitle1 |
30 |
| - case subtitle2 |
31 |
| - case subtitle3 |
32 |
| - |
33 |
| - case body1 |
34 |
| - case body2 |
35 |
| - |
36 |
| - case button0 |
37 |
| - case button1 |
38 |
| - case button2 |
39 |
| - case button3 |
40 |
| - case button4 |
41 |
| - |
42 |
| - case caption0 |
43 |
| - case caption1 |
44 |
| - case caption2 |
45 |
| - |
46 |
| - var uiFont: UIFont { |
47 |
| - switch self { |
48 |
| - case .display1: |
49 |
| - return UIFont.systemFont(ofSize: 40, weight: .bold ) |
50 |
| - case .display2: |
51 |
| - return UIFont.systemFont(ofSize: 32, weight: .bold ) |
52 |
| - case .title1: |
53 |
| - return UIFont.systemFont(ofSize: 28, weight: .bold ) |
54 |
| - case .title2: |
55 |
| - return UIFont.systemFont(ofSize: 24, weight: .bold ) |
56 |
| - case .title3: |
57 |
| - return UIFont.systemFont(ofSize: 20, weight: .bold ) |
58 |
| - case .subtitle1: |
59 |
| - return UIFont.systemFont(ofSize: 20, weight: .semibold ) |
60 |
| - case .subtitle2: |
61 |
| - return UIFont.systemFont(ofSize: 16, weight: .semibold ) |
62 |
| - case .subtitle3: |
63 |
| - return UIFont.systemFont(ofSize: 14, weight: .semibold ) |
64 |
| - case .body1: |
65 |
| - return UIFont.systemFont(ofSize: 15, weight: .regular ) |
66 |
| - case .body2: |
67 |
| - return UIFont.systemFont(ofSize: 14, weight: .regular ) |
68 |
| - case .button0: |
69 |
| - return UIFont.systemFont(ofSize: 16, weight: .medium ) |
70 |
| - case .button1: |
71 |
| - return UIFont.systemFont(ofSize: 16, weight: .semibold ) |
72 |
| - case .button2: |
73 |
| - return UIFont.systemFont(ofSize: 14, weight: .semibold ) |
74 |
| - case .button3: |
75 |
| - return UIFont.systemFont(ofSize: 14, weight: .regular ) |
76 |
| - case .button4: |
77 |
| - return UIFont.systemFont(ofSize: 12, weight: .medium ) |
78 |
| - case .caption0: |
79 |
| - return UIFont.systemFont(ofSize: 12, weight: .semibold ) |
80 |
| - case .caption1: |
81 |
| - return UIFont.systemFont(ofSize: 12, weight: .regular ) |
82 |
| - case .caption2: |
83 |
| - return UIFont.systemFont(ofSize: 11, weight: .regular ) |
84 |
| - } |
85 |
| - } |
86 |
| - } |
| 11 | +extension Text.TruncationMode { |
| 12 | + public static var allCases: [Text.TruncationMode] = [.head, .middle, .tail] |
| 13 | +} |
87 | 14 |
|
88 |
| - public init(text: String, |
89 |
| - typoStyle: LabelTypoStyle = .body1, |
90 |
| - textColor: Color = Color.black, |
91 |
| - maxWidth: CGFloat = .greatestFiniteMagnitude, |
92 |
| - numberOfLines: Int = 0, |
93 |
| - lineBreakMode: NSLineBreakMode = .byWordWrapping, |
94 |
| - alignment: NSTextAlignment = .center, |
95 |
| - lineBreakStrategy: NSParagraphStyle.LineBreakStrategy = .hangulWordPriority) { |
| 15 | +public struct YDSLabel: View { |
| 16 | + let text: String? |
| 17 | + let style: Font |
| 18 | + let lineLimit: Int? |
| 19 | + let textColor: Color |
| 20 | + let alignment: TextAlignment |
| 21 | + let truncationMode: Text.TruncationMode |
| 22 | + let allowsTightening: Bool |
| 23 | + |
| 24 | + public init(text: String? = "Label", |
| 25 | + style: Font = YDSFont.display1, |
| 26 | + lineLimit: Int? = nil, |
| 27 | + textColor: Color = YDSColor.textPrimary, |
| 28 | + alignment: TextAlignment = .center, |
| 29 | + truncationMode: Text.TruncationMode = .tail, |
| 30 | + allowsTightening: Bool = false |
| 31 | + ) { |
96 | 32 | self.text = text
|
97 |
| - self.typoStyle = typoStyle.uiFont |
| 33 | + self.style = style |
| 34 | + self.lineLimit = lineLimit |
98 | 35 | self.textColor = textColor
|
99 |
| - self.maxWidth = maxWidth |
100 |
| - self.numberOfLines = numberOfLines |
101 |
| - self.lineBreakMode = lineBreakMode |
102 | 36 | self.alignment = alignment
|
103 |
| - self.lineBreakStrategy = lineBreakStrategy |
| 37 | + self.truncationMode = truncationMode |
| 38 | + self.allowsTightening = allowsTightening |
104 | 39 | }
|
105 | 40 |
|
106 |
| - public func makeUIView(context: Context) -> UILabel { |
107 |
| - let label = UILabel() |
108 |
| - label.text = text |
109 |
| - label.font = typoStyle |
110 |
| - label.textColor = UIColor(textColor ?? .black) |
111 |
| - label.numberOfLines = numberOfLines ?? 0 |
112 |
| - label.lineBreakMode = lineBreakMode ?? .byWordWrapping |
113 |
| - label.textAlignment = alignment ?? .center |
114 |
| - label.lineBreakStrategy = lineBreakStrategy ?? .hangulWordPriority |
115 |
| - label.preferredMaxLayoutWidth = maxWidth ?? .greatestFiniteMagnitude |
116 |
| - |
117 |
| - return label |
| 41 | + public var body: some View { |
| 42 | + if let text = text { |
| 43 | + Text(text) |
| 44 | + .font(style) |
| 45 | + .lineLimit(lineLimit) |
| 46 | + .foregroundColor(textColor) |
| 47 | + .multilineTextAlignment(alignment) |
| 48 | + .truncationMode(truncationMode) |
| 49 | + .allowsTightening(allowsTightening) |
| 50 | + } |
118 | 51 | }
|
| 52 | +} |
119 | 53 |
|
120 |
| - public func updateUIView(_ uiView: UILabel, context: Context) { |
121 |
| - uiView.text = text |
122 |
| - uiView.font = typoStyle |
123 |
| - uiView.textColor = UIColor(textColor ?? .black) |
124 |
| - uiView.numberOfLines = numberOfLines ?? 0 |
125 |
| - uiView.lineBreakMode = lineBreakMode ?? .byWordWrapping |
126 |
| - uiView.textAlignment = alignment ?? .center |
127 |
| - uiView.lineBreakStrategy = lineBreakStrategy ?? .hangulWordPriority |
128 |
| - uiView.setContentHuggingPriority(.defaultHigh, for: .vertical) |
| 54 | +struct YDSLabel_Previews: PreviewProvider { |
| 55 | + static var previews: some View { |
| 56 | + YDSLabel() |
129 | 57 | }
|
130 | 58 | }
|
0 commit comments