원하는 범위의 문자열에만 색상을 변경하거나 볼드 처리를 하는 등의 작업이 필요할 때가 있다.
요 때 코드상에서 NSMutableAttributedString을 사용해 적용할 수 있는데, 적용할 때마다 찾아보게 돼서 한 번 정리가 필요하다고 느꼈다.
NSMutableAttributedString을 원하는 UILabel의 텍스트로 만들어 NSRange를 통해 원하는 부분만 설정해주었다.
if let text = self.attributedLabel?.text {
// UILabel의 텍스트를 가지고 NSMutableAttributedString 생성
let attributedString = NSMutableAttributedString(string: text)
// 원하는 attribute를 dictionary로 설정
let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.red]
// 설정한 attributes를 원하는 range에 적용되도록 addAttributed 사용
attributedString.addAttributes(attributes, range: NSRange(location: 0, length: 3))
// UILabel의 attributedText에 설정한 string을 set
self.attributedLabel?.attributedText = attributedString
}
range에는 NSRange를 사용해야 한다. 원하는 키워드에 대한 range를 찾아 NSRange로 변환해서 사용해도 될 것 같다. (이것도 별도로 한 번 해봐야겠네..)
NSAttributedString.Key에는 꽤 여러 가지를 설정할 수 있도록 지원해 준다. 아래에 코드블럭으로 추가해 두었다.
extension NSAttributedString.Key {
/************************ Attributes ************************/
@available(iOS 6.0, *)
public static let font: NSAttributedString.Key
@available(iOS 6.0, *)
public static let paragraphStyle: NSAttributedString.Key // NSParagraphStyle, default defaultParagraphStyle
@available(iOS 6.0, *)
public static let foregroundColor: NSAttributedString.Key // UIColor, default blackColor
@available(iOS 6.0, *)
public static let backgroundColor: NSAttributedString.Key // UIColor, default nil: no background
@available(iOS 6.0, *)
public static let ligature: NSAttributedString.Key // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
@available(iOS 6.0, *)
public static let kern: NSAttributedString.Key // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
@available(iOS 14.0, *)
public static let tracking: NSAttributedString.Key // NSNumber containing floating point value, in points; amount to modify default tracking. 0 means tracking is disabled.
@available(iOS 6.0, *)
public static let strikethroughStyle: NSAttributedString.Key // NSNumber containing integer, default 0: no strikethrough
@available(iOS 6.0, *)
public static let underlineStyle: NSAttributedString.Key // NSNumber containing integer, default 0: no underline
@available(iOS 6.0, *)
public static let strokeColor: NSAttributedString.Key // UIColor, default nil: same as foreground color
@available(iOS 6.0, *)
public static let strokeWidth: NSAttributedString.Key // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
@available(iOS 6.0, *)
public static let shadow: NSAttributedString.Key // NSShadow, default nil: no shadow
@available(iOS 7.0, *)
public static let textEffect: NSAttributedString.Key // NSString, default nil: no text effect
@available(iOS 7.0, *)
public static let attachment: NSAttributedString.Key // NSTextAttachment, default nil
@available(iOS 7.0, *)
public static let link: NSAttributedString.Key // NSURL (preferred) or NSString
@available(iOS 7.0, *)
public static let baselineOffset: NSAttributedString.Key // NSNumber containing floating point value, in points; offset from baseline, default 0
@available(iOS 7.0, *)
public static let underlineColor: NSAttributedString.Key // UIColor, default nil: same as foreground color
@available(iOS 7.0, *)
public static let strikethroughColor: NSAttributedString.Key // UIColor, default nil: same as foreground color
@available(iOS 7.0, *)
public static let obliqueness: NSAttributedString.Key // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
@available(iOS 7.0, *)
public static let expansion: NSAttributedString.Key // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion
@available(iOS 7.0, *)
public static let writingDirection: NSAttributedString.Key // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values. LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,
@available(iOS 6.0, *)
public static let verticalGlyphForm: NSAttributedString.Key // An NSNumber containing an integer value. 0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it's always horizontal. The behavior for any other value is undefined.
많이 쓰는 건 font, foregroundColor, backgroundColor 정도 아닐까 싶다.
반응형
'iOS' 카테고리의 다른 글
iOS에서 json 처리하기 (0) | 2023.02.03 |
---|---|
View Life Cycle / Frame and Bounds (0) | 2023.02.03 |
[iOS] UITableView 예제 코드 만들기 2 - Custom UITableViewCell xib 파일로 만들기 (0) | 2021.10.13 |
[iOS] UITableView 예제 코드 만들기 1 (0) | 2021.10.12 |
[iOS] UITableView 알아보기 (2) - UITableViewDelegate, UITableViewDataSource (0) | 2021.10.07 |