Contents

조회 수 1223 댓글 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];



?

  1. [ios] UILabel with two different color text

  2. [ios] 배경에 Gradient 적용하기 (CAGradientLayer)

  3. Brave Browser for Debian, Ubuntu, Mint

  4. Ubuntu Desktop RDP Setup - 24.04 LTS

  5. 원어민이 매일 쓰는 일상표현 150개

  6. 영어. 반드시 외워야 할 문장 패턴 100개

  7. [swift] UIView에서 subview 찾기

  8. 네트워크 용어 정리

  9. [macos] How to Fix ‘You Shut Down Your Computer Because of a Problem’

  10. [ios] 여러 버전의 Xcode 사용하기

  11. [ios] Pod 특정 버전 설치하고 사용하기

  12. 맥에서 포트 확인하고 닫기 (mac)

Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 99 Next
/ 99