Skip to content

Commit ec77db8

Browse files
authored
Merge pull request #168 from PopPool/refactor/#161-font-system
2 parents 51560f7 + bde6ab1 commit ec77db8

File tree

28 files changed

+465
-181
lines changed

28 files changed

+465
-181
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
3+
public extension String {
4+
var isHangul: Bool {
5+
return "\(self)".range(of: "\\p{Hangul}", options: .regularExpression) != nil
6+
}
7+
}

โ€ŽPoppool/PresentationLayer/DesignSystem/DesignSystem/Components/PPButton.swiftโ€Ž

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import UIKit
22

3+
import Infrastructure
4+
35
public class PPButton: UIButton {
46

57
public enum ButtonStyle {
@@ -62,6 +64,29 @@ public class PPButton: UIButton {
6264
}
6365
}
6466

67+
public init(
68+
buttonStyle: ButtonStyle,
69+
fontStyle: PPFontStyle = .KOm16,
70+
text: String,
71+
disabledText: String = " ",
72+
cornerRadius: CGFloat = 4
73+
) {
74+
super.init(frame: .zero)
75+
76+
self.setTitleColor(buttonStyle.textColor, for: .normal)
77+
self.setTitleColor(buttonStyle.disabledTextColor, for: .disabled)
78+
79+
self.setBackgroundColor(buttonStyle.backgroundColor, for: .normal)
80+
self.setBackgroundColor(buttonStyle.disabledBackgroundColor, for: .disabled)
81+
82+
self.setText(to: text, with: fontStyle, for: .normal)
83+
self.setText(to: disabledText, with: fontStyle, for: .disabled)
84+
85+
self.layer.cornerRadius = cornerRadius
86+
self.clipsToBounds = true
87+
}
88+
89+
@available(*, deprecated, message: "PPFontStyle๋กœ ํŒŒ์‹ฑํ•˜๋Š” init์„ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”.")
6590
public init(
6691
style: ButtonStyle,
6792
text: String,
@@ -71,16 +96,22 @@ public class PPButton: UIButton {
7196
) {
7297
super.init(frame: .zero)
7398

74-
self.setTitle(text, for: .normal)
75-
self.setTitle(disabledText, for: .disabled)
99+
guard let parseResult = parseToPPFontStyle(text: text, font: font),
100+
let PPFontStyle = PPFontStyle(rawValue: parseResult)
101+
else {
102+
Logger.log("PPFontStyle๋กœ ํŒŒ์‹ฑํ•  ์ˆ˜ ์—†๋Š” ํฐํŠธ์ž…๋‹ˆ๋‹ค.", category: .error)
103+
return
104+
}
76105

77106
self.setTitleColor(style.textColor, for: .normal)
78107
self.setTitleColor(style.disabledTextColor, for: .disabled)
79108

80109
self.setBackgroundColor(style.backgroundColor, for: .normal)
81110
self.setBackgroundColor(style.disabledBackgroundColor, for: .disabled)
82111

83-
self.titleLabel?.font = font
112+
self.setText(to: text, with: PPFontStyle, for: .normal)
113+
self.setText(to: disabledText, with: PPFontStyle, for: .disabled)
114+
84115
self.layer.cornerRadius = cornerRadius
85116
self.clipsToBounds = true
86117
}
@@ -105,3 +136,26 @@ public class PPButton: UIButton {
105136
self.setBackgroundImage(backgroundImage, for: state)
106137
}
107138
}
139+
140+
private extension PPButton {
141+
func parseToPPFontStyle(text: String?, font: UIFont?) -> String? {
142+
guard let font = font else { return nil }
143+
144+
var result = ""
145+
146+
let splitResult = font.fontName.split(separator: "-")
147+
splitResult[0] == "Poppins" ? result.append("EN") : result.append("KO")
148+
149+
switch splitResult[1] {
150+
case "Light": result.append("l")
151+
case "Regular": result.append("r")
152+
case "Medium": result.append("m")
153+
case "Bold": result.append("b")
154+
default: return nil
155+
}
156+
157+
result.append("\(Int(font.pointSize))")
158+
159+
return result
160+
}
161+
}

โ€ŽPoppool/PresentationLayer/DesignSystem/DesignSystem/Components/PPCancelHeaderView.swiftโ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ public final class PPCancelHeaderView: UIView {
1414

1515
public let cancelButton: UIButton = {
1616
let button = UIButton(type: .system)
17-
button.setTitle("์ทจ์†Œ", for: .normal)
18-
button.titleLabel?.font = .korFont(style: .regular, size: 14)
1917
button.setTitleColor(.black, for: .normal)
18+
button.setText(to: "์ทจ์†Œ", with: .KOr14, for: .normal)
2019
return button
2120
}()
2221

โ€ŽPoppool/PresentationLayer/DesignSystem/DesignSystem/Components/PPLabel.swiftโ€Ž

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,61 @@ import UIKit
22

33
public class PPLabel: UILabel {
44

5+
@available(*, deprecated, renamed: "init(text:style:)", message: "ํƒ€์ดํฌ๋ฅผ ์ง์ ‘ ์ง€์ •ํ•˜๋Š” ๋Œ€์‹  PPFontStyle์„ ์ด์šฉํ•˜์„ธ์š”")
56
public init(
67
style: UIFont.FontStyle = .regular,
78
fontSize: CGFloat = 12,
8-
text: String = "",
9+
text: String = " ",
910
lineHeight: CGFloat = 1.2
1011
) {
1112
super.init(frame: .zero)
12-
self.font = .korFont(style: style, size: fontSize)
13-
let paragraphStyle = NSMutableParagraphStyle()
14-
paragraphStyle.lineHeightMultiple = lineHeight
15-
self.attributedText = NSMutableAttributedString(
16-
string: text,
17-
attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle]
18-
)
13+
// self.font = .korFont(style: style, size: fontSize)
14+
// let paragraphStyle = NSMutableParagraphStyle()
15+
// paragraphStyle.lineHeightMultiple = lineHeight
16+
// self.attributedText = NSMutableAttributedString(
17+
// string: text,
18+
// attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle]
19+
// )
20+
21+
guard let parseResult = parseToPPFontStyle(
22+
style: style,
23+
fontSize: fontSize,
24+
text: text
25+
) else { return }
26+
27+
self.setText(to: text, with: PPFontStyle(rawValue: parseResult) ?? .KOb32)
28+
}
29+
30+
public init(
31+
text: String = " ", // ๊ฐ’์ด ์—†์œผ๋ฉด Attribute๊ฐ€ ์ ์šฉ์ด ์•ˆ๋ผ์„œ ๊ธฐ๋ณธ๊ฐ’์€ ๊ณต๋ฐฑ
32+
style: PPFontStyle
33+
) {
34+
super.init(frame: .zero)
35+
self.setText(to: text, with: style)
1936
}
2037

2138
required init?(coder: NSCoder) {
2239
fatalError("init(coder:) has not been implemented")
2340
}
2441
}
42+
43+
private extension PPLabel {
44+
func parseToPPFontStyle(
45+
style: UIFont.FontStyle,
46+
fontSize: CGFloat,
47+
text: String
48+
) -> String? {
49+
var result = text.isHangul ? "KO" : "EN"
50+
51+
switch style {
52+
case .bold: result.append("b")
53+
case .medium: result.append("m")
54+
case .regular: result.append("r")
55+
case .light: result.append("l")
56+
}
57+
58+
result.append("\(Int(fontSize))")
59+
60+
return result
61+
}
62+
}

โ€ŽPoppool/PresentationLayer/DesignSystem/DesignSystem/Components/PPSearchBarView.swiftโ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ public class PPSearchBarView: UIView {
1212
}
1313

1414
public let searchBar = UISearchBar().then {
15-
$0.searchTextField.setPlaceholder(text: "ํŒ์—…์Šคํ† ์–ด๋ช…์„ ์ž…๋ ฅํ•ด๋ณด์„ธ์š”", color: .g400, font: .korFont(style: .regular, size: 14))
15+
$0.searchTextField.setPlaceholder(
16+
text: "ํŒ์—…์Šคํ† ์–ด๋ช…์„ ์ž…๋ ฅํ•ด๋ณด์„ธ์š”",
17+
color: .g400,
18+
style: .KOr14
19+
)
1620
$0.tintColor = .g400
1721
$0.backgroundColor = .g50
1822
$0.layer.cornerRadius = 4

โ€ŽPoppool/PresentationLayer/DesignSystem/DesignSystem/Components/PPTagCollectionViewCell/PPTagCollectionViewCell.swiftโ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public final class PPTagCollectionViewCell: UICollectionViewCell {
99

1010
public var disposeBag = DisposeBag()
1111

12-
public let titleLabel = PPLabel(style: .medium, fontSize: 11)
12+
public let titleLabel = PPLabel(style: .KOm11)
1313

1414
public let cancelButton = UIButton()
1515

@@ -83,6 +83,7 @@ private extension PPTagCollectionViewCell {
8383
}
8484
}
8585

86+
// FIXME: Chip ์ปดํฌ๋„ŒํŠธํ™” ๋ฐ ๋ถ„๊ธฐ์ ์šฉ ํ•„์š” (๋””์ž์ธ ์‹œ์Šคํ…œ ๋„์ž… ํ•„์š”)
8687
extension PPTagCollectionViewCell {
8788
public func configureCell(title: String? = nil, id: Int?, isSelected: Bool = false, isCancelable: Bool = true, fontSize: CGFloat = 11, cornerRadius: CGFloat = 15.5) {
8889
let xmarkImage = isSelected ? UIImage(named: "icon_xmark_white") : UIImage(named: "icon_xmark_gray")

โ€ŽPoppool/PresentationLayer/DesignSystem/DesignSystem/Components/PopupGridCollectionViewCell/PPPopupGridCollectionViewCell.swiftโ€Ž

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,35 @@ public final class PPPopupGridCollectionViewCell: UICollectionViewCell {
1515
$0.contentMode = .scaleAspectFill
1616
}
1717

18-
private let categoryLabel = PPLabel(style: .bold, fontSize: 11).then {
18+
private let categoryLabel = PPLabel(style: .KOb11).then {
1919
$0.textColor = .blu500
20-
$0.setLineHeightText(text: "category", font: .korFont(style: .bold, size: 11))
2120
}
2221

23-
private let titleLabel = PPLabel(style: .bold, fontSize: 14).then {
22+
private let titleLabel = PPLabel(style: .KOb14).then {
2423
$0.numberOfLines = 2
2524
$0.lineBreakMode = .byTruncatingTail
26-
$0.setLineHeightText(text: "title", font: .korFont(style: .bold, size: 14))
2725
}
2826

29-
private let addressLabel = PPLabel(style: .medium, fontSize: 11).then {
27+
private let addressLabel = PPLabel(style: .KOm11).then {
3028
$0.numberOfLines = 1
3129
$0.lineBreakMode = .byTruncatingTail
3230
$0.textColor = .g400
33-
$0.setLineHeightText(text: "address", font: .korFont(style: .medium, size: 11))
3431
}
3532

36-
private let dateLabel = PPLabel(style: .medium, fontSize: 11).then {
33+
private let dateLabel = PPLabel(style: .ENr11).then {
3734
$0.lineBreakMode = .byTruncatingTail
3835
$0.textColor = .g400
39-
$0.setLineHeightText(text: "date", font: .korFont(style: .medium, size: 11))
4036
}
4137

4238
public let bookmarkButton = UIButton()
4339

44-
private let rankLabel = UILabel().then {
40+
private let rankLabel = PPLabel(style: .KOm11).then {
4541
$0.backgroundColor = .w10
4642
$0.layer.cornerRadius = 12
4743
$0.clipsToBounds = true
4844
$0.isHidden = true
4945
$0.textColor = .w100
5046
$0.textAlignment = .center
51-
$0.setLineHeightText(text: "rank", font: .korFont(style: .medium, size: 11), lineHeight: 1)
5247
}
5348

5449
// MARK: - init
@@ -103,14 +98,12 @@ private extension PPPopupGridCollectionViewCell {
10398

10499
dateLabel.snp.makeConstraints { make in
105100
make.leading.equalToSuperview()
106-
make.height.equalTo(15).priority(.high)
107101
make.bottom.equalToSuperview()
108102
}
109103

110104
addressLabel.snp.makeConstraints { make in
111105
make.leading.trailing.equalToSuperview()
112106
make.bottom.equalTo(dateLabel.snp.top)
113-
make.height.equalTo(17).priority(.high)
114107
}
115108

116109
bookmarkButton.snp.makeConstraints { make in
@@ -137,12 +130,12 @@ private extension PPPopupGridCollectionViewCell {
137130
extension PPPopupGridCollectionViewCell {
138131
public func configureCell(imagePath: String?, id: Int64, category: String?, title: String?, address: String?, startDate: String?, endDate: String?, isBookmark: Bool, isLogin: Bool, isPopular: Bool = false, row: Int?) {
139132

140-
categoryLabel.text = "#" + (category ?? "")
141-
titleLabel.text = title
142-
addressLabel.text = address
133+
categoryLabel.updateText(to: "#" + (category ?? ""))
134+
titleLabel.updateText(to: title)
135+
addressLabel.updateText(to: address)
143136

144137
let date = startDate.toDate().toPPDateString() + " ~ " + endDate.toDate().toPPDateString()
145-
dateLabel.text = date
138+
dateLabel.updateText(to: date)
146139

147140
let bookmarkImage = isBookmark ? UIImage(named: "icon_bookmark_fill") : UIImage(named: "icon_bookmark")
148141
bookmarkButton.setImage(bookmarkImage, for: .normal)
@@ -153,7 +146,7 @@ extension PPPopupGridCollectionViewCell {
153146
rankLabel.isHidden = !isPopular
154147

155148
if let rank = row {
156-
rankLabel.text = "\(rank)"
149+
rankLabel.updateText(to: "\(rank)")
157150
rankLabel.isHidden = rank > 2
158151
}
159152
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import UIKit
2+
3+
public extension UIButton {
4+
/// Style์„ ํ•„์š”๋กœํ•˜๋Š” Text๊ฐ€ ํฌํ•จ๋œ ์ผ๋ฐ˜ ๋ฒ„ํŠผ์—์„œ ์‚ฌ์šฉ
5+
func setText(
6+
to text: String = " ",
7+
with style: PPFontStyle,
8+
for controlState: UIControl.State = .normal
9+
) {
10+
let paragraphStyle = NSMutableParagraphStyle()
11+
paragraphStyle.lineHeightMultiple = style.lineHeightMultiple
12+
paragraphStyle.maximumLineHeight = style.lineHeight
13+
paragraphStyle.minimumLineHeight = style.lineHeight
14+
15+
let attributedString = NSAttributedString(
16+
string: text,
17+
attributes: [
18+
.font: UIFont.PPFont(style: style),
19+
.paragraphStyle: paragraphStyle,
20+
.baselineOffset: style.baseLineOffset
21+
]
22+
)
23+
24+
self.setAttributedTitle(attributedString, for: controlState)
25+
}
26+
}

โ€ŽPoppool/PresentationLayer/DesignSystem/DesignSystem/Extension/UIFont+.swiftโ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public extension UIFont {
2121
if let font = UIFont(name: fontName, size: size) { return font } else { return registerAndGetFont(name: fontName, size: size) }
2222
}
2323

24+
static func PPFont(style: PPFontStyle) -> UIFont {
25+
if let font = UIFont(name: style.fontName, size: style.size) {
26+
return font
27+
} else {
28+
return registerAndGetFont(name: style.fontName, size: style.size)
29+
}
30+
}
31+
2432
private static func registerAndGetFont(name: String, size: CGFloat) -> UIFont {
2533
let url = Bundle.module.url(forResource: name, withExtension: "ttf")!
2634
CTFontManagerRegisterFontURLs([url as CFURL] as CFArray, .process, true, nil)

0 commit comments

Comments
ย (0)