ใช้ NSAttributedString ผสมสไตล์ (Font, Color, Size) ใน UITextView เดียว
โค้ดพื้นฐาน
let textView = UITextView()
textView.isScrollEnabled = true
textView.isEditable = false
textView.frame = CGRect(x: 20, y: 100, width: view.bounds.width - 40, height: 200)
// Attribute 1: Bold Blue
let attr1: [NSAttributedString.Key: Any] = [
.font: UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.systemBlue
]
// Attribute 2: Regular Red
let attr2: [NSAttributedString.Key: Any] = [
.font: UIFont.systemFont(ofSize: 16),
.foregroundColor: UIColor.systemRed
]
// Attribute 3: Italic Gray
let attr3: [NSAttributedString.Key: Any] = [
.font: UIFont.italicSystemFont(ofSize: 14),
.foregroundColor: UIColor.systemGray
]
let string1 = NSAttributedString(string: "Bold Blue Text\n", attributes: attr1)
let string2 = NSAttributedString(string: "Regular Red Text\n", attributes: attr2)
let string3 = NSAttributedString(string: "Italic Gray Text", attributes: attr3)
let combined = NSMutableAttributedString()
combined.append(string1)
combined.append(string2)
combined.append(string3)
textView.attributedText = combined
view.addSubview(textView)
Extension ใช้งานง่าย
extension NSMutableAttributedString {
func append(_ string: String, font: UIFont, color: UIColor) -> NSMutableAttributedString {
let attrs: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: color]
append(NSAttributedString(string: string, attributes: attrs))
return self
}
func append(_ string: String, style: UIFont.TextStyle, color: UIColor, traits: UIFontDescriptor.SymbolicTraits = []) -> NSMutableAttributedString {
var font = UIFont.preferredFont(forTextStyle: style)
if !traits.isEmpty {
font = UIFont(descriptor: font.fontDescriptor.withSymbolicTraits(traits)!, size: 0)
}
return append(string, font: font, color: color)
}
}
// ใช้งาน
let attributed = NSMutableAttributedString()
.append("Title: ", font: .boldSystemFont(ofSize: 18), color: .label)
.append("Description ", font: .systemFont(ofSize: 16), color: .secondaryLabel)
.append("Important!", font: .boldSystemFont(ofSize: 16), color: .systemRed, traits: .traitItalic)
textView.attributedText = attributed
HTML to AttributedString (iOS 7+)
let html = "Bold Italic Red"
if let data = html.data(using: .utf8),
let attributed = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
textView.attributedText = attributed
}
Markdown Support (iOS 15+)
// ใช้ AttributedString + Markdown
var attributed = AttributedString("**Bold** *Italic* `Code`")
textView.attributedText = NSAttributedString(attributed)
Interactive Elements
// Link
let linkAttrs: [NSAttributedString.Key: Any] = [
.link: URL(string: "https://example.com")!,
.foregroundColor: UIColor.systemBlue,
.underlineStyle: NSUnderlineStyle.single.rawValue
]
attributed.append(NSAttributedString(string: "Tap here", attributes: linkAttrs))
textView.isSelectable = true
textView.isEditable = false
textView.dataDetectorTypes = .link // Auto-detect Links, Phone, Address
textView.delegate = self // UITextViewDelegate สำหรับ shouldInteractWith URL