Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/adjust line height #58

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion ResourceKit/Sources/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// Copyright ยฉ 2023 chansoo.MOIT. All rights reserved.
//

import Foundation
// TODO: extension ๋ฉ”์„œ๋“œ ์œ„์น˜ ์ด๋™ ํ›„ Foundation์œผ๋กœ ๋ณ€๊ฒฝ ํ•„์š”
import UIKit

extension ResourceKitFontFamily {
public static let h1 = Pretendard.bold.font(size: 36)
Expand All @@ -19,4 +20,46 @@ extension ResourceKitFontFamily {
public static let p2 = Pretendard.medium.font(size: 14)
public static let p3 = Pretendard.regular.font(size: 14)
public static let caption = Pretendard.medium.font(size: 12)

public static func lineHeight(of font: ResourceKitFontConvertible.Font) -> CGFloat {
if font == h1 { return 50 }
else if [h2, h4].contains(where: { $0 == font }) { return 32 }
else if font == h3 { return 36 }
else if font == h5 { return 27}
else if [h6, p1].contains(where: { $0 == font }) { return 23 }
else if [p2, p3].contains(where: { $0 == font }) { return 22 }
else if font == caption { return 18 }
else { return 0 }
}
}

// TODO: ์ถ”ํ›„ Utils๋กœ ์œ„์น˜ ์ด๋™ ํ•„์š”
public extension UILabel {
func setTextWithParagraphStyle(
text: String,
alignment: NSTextAlignment = .left,
font: ResourceKitFontConvertible.Font,
textColor: UIColor
) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = alignment

let fontHeight = ResourceKitFontFamily.lineHeight(of: font)
paragraphStyle.maximumLineHeight = fontHeight
paragraphStyle.minimumLineHeight = fontHeight

let attributes: [NSAttributedString.Key : Any] = [
.paragraphStyle : paragraphStyle,
.font: font,
.foregroundColor: textColor,
.baselineOffset: (fontHeight - font.lineHeight) / 4
]

debugPrint(font.lineHeight)

let attrString = NSAttributedString(string: text,
attributes: attributes)
self.attributedText = attrString
}
}