Bibi's DevLog ๐Ÿค“๐ŸŽ

UILabel ํ…์ŠคํŠธ์˜ ์ผ๋ถ€ ํฐํŠธ/์ƒ‰์ƒ/ํฌ๊ธฐ ๋ณ€๊ฒฝํ•˜๊ธฐ, ์ทจ์†Œ์„  ๊ธ‹๊ธฐ - attributedText, NSMutableAttributedString ๋ณธ๋ฌธ

๐Ÿ“ฑ๐ŸŽ iOS

UILabel ํ…์ŠคํŠธ์˜ ์ผ๋ถ€ ํฐํŠธ/์ƒ‰์ƒ/ํฌ๊ธฐ ๋ณ€๊ฒฝํ•˜๊ธฐ, ์ทจ์†Œ์„  ๊ธ‹๊ธฐ - attributedText, NSMutableAttributedString

๋น„๋น„ bibi 2022. 5. 14. 00:19

UILabel ํ…์ŠคํŠธ์˜ ์ผ๋ถ€ ํฐํŠธ/์ƒ‰์ƒ/ํฌ๊ธฐ ๋ณ€๊ฒฝํ•˜๊ธฐ, ์ทจ์†Œ์„  ๊ธ‹๊ธฐ - attributedText, NSMutableAttributedString

https://ios-development.tistory.com/654

https://zeddios.tistory.com/300

  • NSMutableAttributedString(string:) ๊ณผ .addAttribute(_:value:range:)์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•˜๋Š” ๊ฒŒ ์ค‘์š”ํ•˜๋‹ค.
  1. ํ‰์†Œ๋Œ€๋กœ UILabel์„ ๋งŒ๋“ค๊ณ  ํ…์ŠคํŠธ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค
  2. ํ•จ์ˆ˜ (์—ฌ๊ธฐ์„œ๋Š” setMenuLabelAttribute())๋ฅผ ๋งŒ๋“ ๋‹ค
  3. menuLabel.text๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  (fullText)
  4. fullText๋ฅผ NSString์œผ๋กœ ํ˜•๋ณ€ํ™˜ํ•œ๋‹ค
    1. ์•„๋ž˜ ๋‹จ๊ณ„์—์„œ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด String->NSString์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ.
  5. fullTextNSString์„ NSMutableAttributedString(string:) ์„ ํ†ตํ•ด ํ˜•๋ณ€ํ™˜ํ•œ๋‹ค
  6. ๋ณ€๊ฒฝ์„ ์›ํ•˜๋Š” ์†์„ฑ์„ ์ •์˜ํ•œ๋‹ค
    1. let font = UIFont.customFont(.santanaBlackMedium)
  7. attributedString์— ํฐํŠธ/์ƒ‰์ƒ/ํฌ๊ธฐ/์ทจ์†Œ์„  ๊ธ‹๊ธฐ ๋“ฑ ์›ํ•˜๋Š” ์†์„ฑ์„ ์›ํ•˜๋Š” ๋ฒ”์œ„์— ์ ์šฉํ•œ๋‹ค.
    1. attributedString.addAttribute(.font, value: font, range: fullTextNSString.range(of: "1")) : fullTestNSString ์ค‘ "1"์ด๋ผ๋Š” ๊ธ€์ž์— font๋ฅผ ์ ์šฉํ•จ
    2. attributedString.addAttribute(.foregroundColor, value: UIColor.customColor(.primaryGreen), range: fullTextNSString.range(of: "1")) : fullTestNSString ์ค‘ "1"์ด๋ผ๋Š” ๊ธ€์ž์— primaryGreen์ด๋ผ๋Š” ์ƒ‰์ƒ์„ ์ ์šฉํ•จ
    3. ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ์†์„ฑ์€ NSAttributedString.Key๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค.
  8. 1์—์„œ ์„ ์–ธํ•œ UILabel์˜ attributedText๊ฐ’์œผ๋กœ attributedString์„ ๋„ฃ์–ด ์ค€๋‹ค
    1. 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
    }
}