티스토리 뷰


UILabel과 UIFont에 Extension을 추가해서 위와 같은 label을 만들어보자.

 

스토리 보드에서 StackView에 UILabel 두 개를 추가해주었다.

나머지 작업은 모두 코드로 진행한다.

 

 

UIFont+Ext.swift

import UIKit

enum SFProFontStyle {
    case regular, semibold, bold, light
}

extension UIFont {
    func SFProFont(style: SFProFontStyle, size: CGFloat) -> UIFont {
        var font = ""
        
        switch style {
        case .regular:
            font = "SFPro-Regular"
        case .semibold:
            font = "SFPro-Semibold"
        case .bold:
            font = "SFPro-Bold"
        case .light:
            font = "SFPro-Light"
        }
        
        return UIFont(name: font, size: size) ?? UIFont.systemFont(ofSize: size)
    }
}

먼저 UIFont에 SFProFont 함수를 추가해 주었다. 

SFPro 폰트를 사용하며 폰트의 스타일(.regular, semibold, bold... ), 사이즈를 가진 폰트를 리턴하는 함수이다.

 

여기서 리턴받은 폰트를 UILabel에 적용시켜보자.

 

 

UILabel+Ext.swift

import UIKit

extension UILabel {

    func setSFProText(
        text: String = "텍스트",
        style: SFProFontStyle  = .regular,
        size: CGFloat = 11.0,
        color: UIColor = .black)
    {
        self.text = text
        self.font = font.SFProFont(style: style, size: size)
        self.textColor = color
    }
}

setSFProText()에서 text, style, size, color를 입력받고 적용시켜주었다.

 

 

ViewController.swift

func setUpTitle() {
    titleLabel.setSFProText(text: "Enter your payment details", style: .bold, size: 25, color: .black)
    subTitleLabel.setSFProText(text: "By continuing you agree to our Terms", style: .light, size: 20, color: .black)
}

 

 

 

+추가로 문자열에서 특정 문자만 Color를 변경해주자.

 

 

UILabel+Ext.swift

func highlihgtTextColor (targetString: String, color: UIColor) {
    let fullText = text ?? ""
    let attributedString = NSMutableAttributedString(string: fullText)
    let range = (fullText as NSString).range(of: targetString)
    attributedString.addAttribute(.foregroundColor, value: color, range: range)
    // attributedString.addAttribute(.kern, value: 10, range: range) // 글간
    attributedText = attributedString
}

NSMutableAttributedString 객체를 만든 후 range로 문자열에서 문자를 특정한다.

NSMutableAttributedString 객체에 .foregroundColor, .font, .kern(글간)등 변경하고 싶은 속성과 값을 추가해준다.

 

UILabel의 attributedText 속성에 변경한 객체를 추가한다.

 

 

ViewController.swift

func setUpTitle() {
    titleLabel.setSFProText(text: "Enter your payment details", style: .bold, size: 25, color: .black)
    subTitleLabel.setSFProText(text: "By continuing you agree to our Terms", style: .light, size: 20, color: .black)
    
    subTitleLabel.highlihgtTextColor(targetString: "Terms", color: .red)
}

 

 

 


references)

 

[iOS - swift] UILabel에서 특정 문자열의 font, 색상 변경 방법 (attributedText)

Attributed text 개념 UILabel은 attributedText 프로퍼티를 가지고 소유 이름 그대로 Text에 '속성'값이 존재하는 프로퍼티 UILabel의 text에 font, color같은 속성값을 지정할수 있다는 의미 사용 방법 NSAttr..

ios-development.tistory.com

 

'iOS' 카테고리의 다른 글

iOS) Naver 지도 SDK 사용하기  (0) 2022.10.05
iOS) 스토리보드 없이 개발 준비하기  (0) 2022.10.03
iOS) UIView에 Border 적용하기  (0) 2022.09.01
iOS) Realm 사용하기  (0) 2022.08.30
iOS) CoreData 사용하기 (CRUD 구현)  (0) 2022.08.28
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함