iOS
[attributedString] UILabel의 텍스트 커스텀하기 - font, underline, strike, alignment, lineHeight, paragraphStyle
Applecider
2022. 12. 21. 07:00
나중에 복붙해서 사용하려고UILabel의 attributedString 설정값을 간단히 정리해봤다.
private lazy var customLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail
paragraphStyle.lineBreakStrategy = .hangulWordPriority
paragraphStyle.alignment = .center
paragraphStyle.minimumLineHeight = 21
// ✅ NSMutableAttributedString 및 paragraphStyle 활용
let fullText = "안녕하세요. 애플사이다의 iOS 개발일지 블로그에 오셨네요. 메리 크리스마스. 저는 이따 아바타2 보러가요. 그럼 이만!"
let attributedText = NSMutableAttributedString(
string: fullText,
attributes: [.foregroundColor: UIColor.darkGray,
.font: UIFont.systemFont(ofSize: 18, weight: .bold),
.underlineStyle: NSUnderlineStyle.single.rawValue,
// .strikethroughStyle: NSUnderlineStyle.single.rawValue,
// .strikethroughColor: UIColor.systemRed,
.paragraphStyle: paragraphStyle])
// ✅ 텍스트 일부에 효과주기
attributedText.addAttribute(.font,
value: UIFont.systemFont(ofSize: 11),
range:(fullText as NSString).range(of: "그럼 이만!"))
label.attributedText = attributedText
return label
}()
- Reference
- 🍎 예제 코드 > GitHub Repo
- Apple Developer Documentation > UILabel
- 관련 포스트
🍎 포스트가 도움이 되었다면, 공감🤍 / 구독🍹 / 공유🔗 / 댓글✏️ 로 응원해주세요. 감사합니다.