Contents

조회 수 331 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

# 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];



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
» Develop [ios] UILabel with two different color text hooni 2024.12.14 331
842 Develop [ios] 배경에 Gradient 적용하기 (CAGradientLayer) file hooni 2024.12.14 296
841 Develop [swift] UIView에서 subview 찾기 hooni 2022.12.09 3131
840 Develop [ios] 여러 버전의 Xcode 사용하기 hooni 2022.05.28 1775
839 Develop [ios] Pod 특정 버전 설치하고 사용하기 hooni 2022.05.28 2827
838 Develop macOS에 node, npm 설치하기 (homebrew) file hooni 2021.11.06 2594
837 Develop [iOS] 시뮬레이터에 푸시 알림을 보내는 방법 file hooni 2021.10.13 2887
836 Develop [iOS] Xcode 불필요한 캐시 삭제하기 hooni 2021.10.12 2811
835 Develop [swift] 실행시간 측정하기 hooni 2021.09.14 1608
834 Develop [vim] vim 명령으로  문자 제거하기 (remove 65279 bomb) hooni 2021.02.03 2350
833 Develop [js] show/hide 이벤트 감시 (Observing show/hide event) hooni 2021.02.03 3591
832 Develop [swift] popToRoot 모달뷰, 네비게이션컨트롤러 한꺼번에 닫기 file hooni 2021.01.29 2416
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 71 Next
/ 71