Develop
2024.12.14 01:35
[ios] UILabel with two different color text
조회 수 16 댓글 0
# Usage
// Sample private lazy var titleLabel: UILabel = { let label = UILabel() label.textColor = UIColor.lightGray label.font = UIFont.systemFont(ofSize: 16) label.numberOfLines = 0 return label }() let red = "red" let blue = "blue" let green = "green" let stringValue = "\(red)\n\(blue)\n&\n\(green)" let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue) attributedString.setColor(color: UIColor.red, forText: red) // or use direct value for text "red" attributedString.setColor(color: UIColor.green, forText: green) // or use direct value for text "blue" attributedString.setColor(color: UIColor.blue, forText: blue) // or use direct value for text "green" attributedString.setFont(font: UIFont.systemFont(ofSize: 24), forText: green) label.attributedText = attributedString
# Extension Sample
// Extension. extension NSMutableAttributedString { func setColor(color: UIColor, forText stringValue: String) { let range: NSRange = self.mutableString.range(of: stringValue, options: .caseInsensitive) self.addAttribute(NSAttributedString.Key.foregroundColor, value: color, range: range) } func setFont(font: UIFont, forText stringValue: String) { let range: NSRange = self.mutableString.range(of: stringValue, options: .caseInsensitive) self.addAttribute(NSAttributedString.Key.font, value: font, range: range) } }
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
» | Develop | [ios] UILabel with two different color text | hooni | 2024.12.14 | 16 |
1176 | Develop | [ios] 배경에 Gradient 적용하기 (CAGradientLayer) | hooni | 2024.12.14 | 17 |
1175 | System/OS | Brave Browser for Debian, Ubuntu, Mint | hooni | 2024.10.23 | 206 |
1174 | System/OS | Ubuntu Desktop RDP Setup - 24.04 LTS | hooni | 2024.10.16 | 189 |
1173 | Etc | 원어민이 매일 쓰는 일상표현 150개 | hooni | 2023.09.17 | 2351 |
1172 | Etc | 영어. 반드시 외워야 할 문장 패턴 100개 | hooni | 2023.06.08 | 2308 |
1171 | Develop | [swift] UIView에서 subview 찾기 | hooni | 2022.12.09 | 2923 |
1170 | System/OS | 네트워크 용어 정리 | hooni | 2022.11.20 | 2494 |
1169 | System/OS | [macos] How to Fix ‘You Shut Down Your Computer Because of a Problem’ | hooni | 2022.06.01 | 1650 |
1168 | Develop | [ios] 여러 버전의 Xcode 사용하기 | hooni | 2022.05.28 | 1464 |
1167 | Develop | [ios] Pod 특정 버전 설치하고 사용하기 | hooni | 2022.05.28 | 2563 |
1166 | System/OS | 맥에서 포트 확인하고 닫기 (mac) | hooni | 2022.03.22 | 2347 |