Develop
2024.12.14 01:35
[ios] UILabel with two different color text
조회 수 7935 댓글 0
# Usage (Swift)
// 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 (Swift)
// 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) } }
# Example (Objective-C)
// Objective-C Example. NSString *title = @"I am a great student"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:title]; NSRange targetRange = [title rangeOfString:@"great"]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:targetRange]; UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.font = [UIFont systemFontOfSize:16]; titleLabel.textColor = [UIColor lightGrayColor]; titleLabel.numberOfLines = 0; titleLabel.lineBreakMode = NSLineBreakByWordWrapping; titleLabel.textAlignment = NSTextAlignmentLeft; [titleLabel setAttributedText:attributedString];
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|---|
| 974 | Develop |
[ppt] iOS 플라랩#04(2015.06.19) 발표 자료
|
hooni | 2015.06.04 | 3592 |
| 973 | Develop | Aspect Oriented Programming in Objective-C | hooni | 2015.05.18 | 5901 |
| 972 | Database | [mysql] MySql 에서 정렬 후 그룹 하는 방법 | hooni | 2015.05.07 | 7102 |
| 971 | Etc | 인증서 *.p12 파일을 *.pem 파일로 변환 | hooni | 2015.04.30 | 5447 |
| 970 | Etc |
IPR 특허 관련
|
hooni | 2015.04.28 | 0 |
| 969 | Develop | [ios] 설정에서 푸시 알림(APNS) on/off 상태 확인 | hooni | 2015.04.28 | 5463 |
| 968 | Develop | [ios] GPS 이용 상태 확인 | hooni | 2015.04.27 | 4875 |
| 967 | Develop | [js] jQuery, Javascript 모바일(스마트폰) 판단하는 방법 | hooni | 2015.04.26 | 5951 |
| 966 | PPT |
[ppt] iOS 플라랩#03(2015.04.27) 발표 자료
|
hooni | 2015.04.24 | 4209 |
| 965 | PPT |
[ppt] iOS 플라랩#02(2015.03.19) 발표 자료
|
hooni | 2015.04.24 | 4065 |
| 964 | Etc | WM미통기 - 10. 조건부확률 | hooni | 2015.04.20 | 4133 |
| 963 | Etc | 수리통계학 : 표본공간과 사상-1 | hooni | 2015.04.20 | 4000 |