Bibi's DevLog ๐ค๐
UILabel ํ ์คํธ์ ์ผ๋ถ ํฐํธ/์์/ํฌ๊ธฐ ๋ณ๊ฒฝํ๊ธฐ, ์ทจ์์ ๊ธ๊ธฐ - attributedText, NSMutableAttributedString ๋ณธ๋ฌธ
๐ฑ๐ iOS
UILabel ํ ์คํธ์ ์ผ๋ถ ํฐํธ/์์/ํฌ๊ธฐ ๋ณ๊ฒฝํ๊ธฐ, ์ทจ์์ ๊ธ๊ธฐ - attributedText, NSMutableAttributedString
๋น๋น bibi 2022. 5. 14. 00:19UILabel ํ ์คํธ์ ์ผ๋ถ ํฐํธ/์์/ํฌ๊ธฐ ๋ณ๊ฒฝํ๊ธฐ, ์ทจ์์ ๊ธ๊ธฐ - attributedText, NSMutableAttributedString
NSMutableAttributedString(string:)
๊ณผ.addAttribute(_:value:range:)
์ ๊ฐ๋ ์ ์ดํดํ๋ ๊ฒ ์ค์ํ๋ค.
- ํ์๋๋ก UILabel์ ๋ง๋ค๊ณ ํ ์คํธ ๊ฐ์ ๋ฃ๋๋ค
- ํจ์ (์ฌ๊ธฐ์๋
setMenuLabelAttribute()
)๋ฅผ ๋ง๋ ๋ค menuLabel.text
๋ฅผ ๊ฐ์ ธ์ค๊ณ (fullText
)fullText
๋ฅผ NSString์ผ๋ก ํ๋ณํํ๋ค- ์๋ ๋จ๊ณ์์ ๋ฒ์๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํด String->NSString์ผ๋ก ๋ณํํ๋ ๊ฒ.
fullTextNSString
์ NSMutableAttributedString(string:) ์ ํตํด ํ๋ณํํ๋ค- ๋ณ๊ฒฝ์ ์ํ๋ ์์ฑ์ ์ ์ํ๋ค
let font = UIFont.customFont(.santanaBlackMedium)
attributedString
์ ํฐํธ/์์/ํฌ๊ธฐ/์ทจ์์ ๊ธ๊ธฐ ๋ฑ ์ํ๋ ์์ฑ์ ์ํ๋ ๋ฒ์์ ์ ์ฉํ๋ค.attributedString.addAttribute(.font, value: font, range: fullTextNSString.range(of: "1"))
:fullTestNSString
์ค "1"์ด๋ผ๋ ๊ธ์์ font๋ฅผ ์ ์ฉํจattributedString.addAttribute(.foregroundColor, value: UIColor.customColor(.primaryGreen), range: fullTextNSString.range(of: "1"))
:fullTestNSString
์ค "1"์ด๋ผ๋ ๊ธ์์ primaryGreen์ด๋ผ๋ ์์์ ์ ์ฉํจ- ๋ณ๊ฒฝ ๊ฐ๋ฅํ ์์ฑ์
NSAttributedString.Key
๋ฅผ ์ฐธ๊ณ ํ๋ค.
- 1์์ ์ ์ธํ UILabel์ attributedText๊ฐ์ผ๋ก
attributedString
์ ๋ฃ์ด ์ค๋คmenuLabel.attributedText = attributedString
import UIKit
class HomeView: UIView {
static let identifier = "HomeView"
private let menuLabel: UILabel = {
var label = UILabel()
label.text = "1 Sample Menu"
label.font = UIFont.customFont(.santanaBlackSmall)
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
setUI()
setConstraint()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setUI()
setConstraint()
}
private func setUI() {
self.addSubview(menuLabel)
setMenuLabelAttribute()
}
private func setConstraint() {
configureMenuLabelConstraint()
}
private func configureMenuLabelConstraint() {
menuLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
menuLabel.topAnchor.constraint(equalTo: menuImageView.bottomAnchor),
menuLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
menuLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor)
])
}
private func setMenuLabelAttribute() {
let fullText = menuLabel.text ?? ""
let fullTextNSString = fullText as NSString
let attributedString = NSMutableAttributedString(string: fullText)
let font = UIFont.customFont(.santanaBlackMedium)
attributedString.addAttribute(.font, value: font, range: fullTextNSString.range(of: "1"))
attributedString.addAttribute(.foregroundColor, value: UIColor.customColor(.primaryGreen), range: fullTextNSString.range(of: "1"))
menuLabel.attributedText = attributedString
}
}
'๐ฑ๐ iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Environment : ๊ฐ์ฒด์ ์์กด์ฑ ํ๊ฒฝ ๋ง๋ค๊ธฐ (0) | 2022.10.21 |
---|---|
[๊ณฐํ๊น๋] RxSwift + MVVM ์ ๋ฆฌ (0) | 2022.06.06 |
์ฝ๋๋ก UICollectionView ๋ง๋ค๊ธฐ (0) | 2022.05.12 |
TDD, ๋จ์ ํ ์คํธ, XCTest๋ก iOS ์ฑ ํ ์คํธํ๊ธฐ (0) | 2022.05.09 |
UIScrollView ์ฝ๋๋ก ๋ง๋ค๊ธฐ (0) | 2022.04.22 |