Skip to content

Commit

Permalink
Feature/adjust line height (#58)
Browse files Browse the repository at this point in the history
* feat: font 타입 별 line height return 해주는 메서드 구현

* feat: ParagraphStyle 적용하는 UILabel Extension 구현
  • Loading branch information
hope1053 committed Jun 27, 2023
1 parent e256b51 commit 74b0f88
Showing 1 changed file with 44 additions and 1 deletion.
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
}
}

0 comments on commit 74b0f88

Please sign in to comment.