[Swift] Label text 부분적으로 색상 변경하는 방법
UILabel Text 부분적으로 색상 변경하는 방법
Record |
|
작성일 |
2019. 09. 19 (목) |
Swift 버전 |
Swift 5 |
Xcode 버전 |
10.3 |
안녕하세요.
Fury입니다 :]
요즘 공모전에 나가기 위해서
열심히 코딩을 하고 있는데요!!!
간혹 가다 보면 그런 거 있잖아요.
부분적으로 Bold 처리가 되어 있다던가.
아니면 아래 이미지처럼
Text가 있는데 부분적으로 색이 다르다던가.
네. 저는 처음에 부분적으로 다르니
Label을 여러 개 만들어서 처리를 해줬는데요..
왜 그랬나 싶네요..
지금부터 그 방법을 공부해 볼게요!!
1. NSMutableAttributedString 설정 및 적용 |
"NSMutableAttributedString"을 사용할 예정이에요.
아래 설명을 보면
visual style, hyperlinks, or accessibility data와 같은 속성들을 포함하고 있는
변경 가능한 string object라고 설명이 되어 있어요.
아마 저희가 visual style 쪽을 건들면 될 것 같아요.
위의 이미지처럼
포인트는 오렌지색, 보너스는 파란색이 나오도록 할 거예요.
바로 코드를 통해서 살펴볼게요.
주석 내용 그대로입니다!!
attribted 즉, 설정을 text에 넣어주고
Label의 text에 값을 입력하는 것이 아닌
Label의 attributedText에 text를 넣어주시면 끝!!
그 밖에
Font, FontSize 등 다양한 속성들을 변경할 수 있는 게 많으니
한 번 찾아보시길 바랍니다.
https://developer.apple.com/documentation/foundation/nsattributedstring/key
NSAttributedString.Key - NSAttributedString | Apple Developer Documentation
Misspelled text that is visibly marked as misspelled (NSNumber as a Boolean value). If you’re implementing a custom text-editing app, use NSAccessibilityMarkedMisspelledTextAttribute to ensure that VoiceOver properly identifies misspelled text to users.
developer.apple.com