Contents

조회 수 1026 댓글 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

    Date2024.12.14 CategoryDevelop Byhooni Views1026
    Read More
  2. [ios] 배경에 Gradient 적용하기 (CAGradientLayer)

    Date2024.12.14 CategoryDevelop Byhooni Views685
    Read More
  3. Brave Browser for Debian, Ubuntu, Mint

    Date2024.10.23 CategorySystem/OS Byhooni Views1391
    Read More
  4. Ubuntu Desktop RDP Setup - 24.04 LTS

    Date2024.10.16 CategorySystem/OS Byhooni Views852
    Read More
  5. 원어민이 매일 쓰는 일상표현 150개

    Date2023.09.17 CategoryEtc Byhooni Views2931
    Read More
  6. 영어. 반드시 외워야 할 문장 패턴 100개

    Date2023.06.08 CategoryEtc Byhooni Views2806
    Read More
  7. [swift] UIView에서 subview 찾기

    Date2022.12.09 CategoryDevelop Byhooni Views3429
    Read More
  8. 네트워크 용어 정리

    Date2022.11.20 CategorySystem/OS Byhooni Views3006
    Read More
  9. [macos] How to Fix ‘You Shut Down Your Computer Because of a Problem’

    Date2022.06.01 CategorySystem/OS Byhooni Views2584
    Read More
  10. [ios] 여러 버전의 Xcode 사용하기

    Date2022.05.28 CategoryDevelop Byhooni Views2154
    Read More
  11. [ios] Pod 특정 버전 설치하고 사용하기

    Date2022.05.28 CategoryDevelop Byhooni Views3253
    Read More
  12. 맥에서 포트 확인하고 닫기 (mac)

    Date2022.03.22 CategorySystem/OS Byhooni Views3674
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 99 Next
/ 99