
คำตอบนี้ได้รับการปรับปรุงสำหรับ Swift 4.2
อ้างอิงด่วน
รูปแบบทั่วไปสำหรับการสร้างและการตั้งค่าสตริงที่ประกอบเป็นเช่นนี้ คุณสามารถค้นหาตัวเลือกทั่วไปอื่น ๆ ด้านล่าง
// create attributed string
let myString = "Swift Attributed String"
let myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
// set attributed text on a UILabel
myLabel.attributedText = myAttrString

let myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.blue ]

let myAttribute = [ NSAttributedString.Key.backgroundColor: UIColor.yellow ]

let myAttribute = [ NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 18.0)! ]

let myAttribute = [ NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue ]

let myShadow = NSShadow()
myShadow.shadowBlurRadius = 3
myShadow.shadowOffset = CGSize(width: 3, height: 3)
myShadow.shadowColor = UIColor.gray
let myAttribute = [ NSAttributedString.Key.shadow: myShadow ]
ส่วนที่เหลือของโพสต์นี้ให้รายละเอียดเพิ่มเติมสำหรับผู้ที่สนใจ
คุณลักษณะ
แอตทริบิวต์ของสตริงเป็นเพียงพจนานุกรมในรูปแบบ[NSAttributedString.Key: Any]โดยที่NSAttributedString.Keyเป็นชื่อคีย์ของแอตทริบิวต์และAnyเป็นค่าของบางประเภท ค่าอาจเป็นแบบอักษรสีจำนวนเต็มหรืออย่างอื่น มีคุณสมบัติมาตรฐานมากมายใน Swift ที่ได้ถูกกำหนดไว้แล้ว ตัวอย่างเช่น:
- ชื่อคีย์:
NSAttributedString.Key.font , ค่า:UIFont
- ชื่อคีย์:
NSAttributedString.Key.foregroundColor , ค่า:UIColor
- ชื่อคีย์:
NSAttributedString.Key.linkค่า: NSURLหรือNSString
มีอีกหลายคน ดูลิงค์นี้สำหรับข้อมูลเพิ่มเติม คุณสามารถสร้างแอตทริบิวต์ที่กำหนดเองของคุณเองเช่น:
ชื่อคีย์: NSAttributedString.Key.myName, ค่า: บางประเภท
หากคุณทำส่วนขยาย :
extension NSAttributedString.Key {
static let myName = NSAttributedString.Key(rawValue: "myCustomAttributeKey")
}
การสร้างคุณลักษณะใน Swift
คุณสามารถประกาศคุณสมบัติเช่นเดียวกับการประกาศพจนานุกรมอื่น ๆ
// single attributes declared one at a time
let singleAttribute1 = [ NSAttributedString.Key.foregroundColor: UIColor.green ]
let singleAttribute2 = [ NSAttributedString.Key.backgroundColor: UIColor.yellow ]
let singleAttribute3 = [ NSAttributedString.Key.underlineStyle: NSUnderlineStyle.double.rawValue ]
// multiple attributes declared at once
let multipleAttributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: UIColor.green,
NSAttributedString.Key.backgroundColor: UIColor.yellow,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.double.rawValue ]
// custom attribute
let customAttribute = [ NSAttributedString.Key.myName: "Some value" ]
หมายเหตุ rawValueว่าจำเป็นสำหรับค่าสไตล์ขีดเส้นใต้
เนื่องจากคุณลักษณะเป็นเพียงพจนานุกรมคุณยังสามารถสร้างได้โดยสร้างพจนานุกรมว่างแล้วเพิ่มคู่คีย์ - ค่าลงในพจนานุกรม หากค่าจะมีหลายประเภทคุณจะต้องใช้Anyเป็นประเภท นี่คือmultipleAttributesตัวอย่างจากด้านบนสร้างขึ้นในแบบนี้:
var multipleAttributes = [NSAttributedString.Key : Any]()
multipleAttributes[NSAttributedString.Key.foregroundColor] = UIColor.green
multipleAttributes[NSAttributedString.Key.backgroundColor] = UIColor.yellow
multipleAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.double.rawValue
ประกอบสาย
เมื่อคุณเข้าใจคุณลักษณะแล้วคุณสามารถสร้างสตริงที่มีการเชื่อมโยงได้
การเริ่มต้น
มีสองสามวิธีในการสร้างสตริงที่ประกอบ NSAttributedStringหากคุณเพียงแค่ต้องสตริงอ่านอย่างเดียวคุณสามารถใช้ ต่อไปนี้เป็นวิธีเริ่มต้น:
// Initialize with a string only
let attrString1 = NSAttributedString(string: "Hello.")
// Initialize with a string and inline attribute(s)
let attrString2 = NSAttributedString(string: "Hello.", attributes: [NSAttributedString.Key.myName: "A value"])
// Initialize with a string and separately declared attribute(s)
let myAttributes1 = [ NSAttributedString.Key.foregroundColor: UIColor.green ]
let attrString3 = NSAttributedString(string: "Hello.", attributes: myAttributes1)
NSMutableAttributedStringถ้าคุณจะต้องเปลี่ยนแอตทริบิวต์หรือเนื้อหาสตริงในภายหลังคุณควรใช้ การประกาศคล้ายกันมาก:
// Create a blank attributed string
let mutableAttrString1 = NSMutableAttributedString()
// Initialize with a string only
let mutableAttrString2 = NSMutableAttributedString(string: "Hello.")
// Initialize with a string and inline attribute(s)
let mutableAttrString3 = NSMutableAttributedString(string: "Hello.", attributes: [NSAttributedString.Key.myName: "A value"])
// Initialize with a string and separately declared attribute(s)
let myAttributes2 = [ NSAttributedString.Key.foregroundColor: UIColor.green ]
let mutableAttrString4 = NSMutableAttributedString(string: "Hello.", attributes: myAttributes2)
การเปลี่ยนสายอักขระ Attributed
ตัวอย่างเช่นลองสร้างสายอักขระประกอบที่ด้านบนของโพสต์นี้
ขั้นแรกให้สร้างNSMutableAttributedStringด้วยคุณลักษณะแบบอักษรใหม่
let myAttribute = [ NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 18.0)! ]
let myString = NSMutableAttributedString(string: "Swift", attributes: myAttribute )
หากคุณกำลังทำงานอยู่ให้ตั้งค่าสตริงที่ประกอบเป็นUITextView(หรือUILabel) ดังนี้:
textView.attributedText = myString
คุณไม่ได้textView.textใช้
นี่คือผลลัพธ์:

จากนั้นผนวกสตริงอื่นที่ประกอบขึ้นซึ่งไม่มีชุดคุณลักษณะใด ๆ (โปรดสังเกตว่าแม้ว่าฉันเคยletประกาศmyStringข้างต้นฉันยังคงสามารถแก้ไขได้เพราะมันเป็นสิ่งNSMutableAttributedStringนี้ดูเหมือนจะค่อนข้างไม่เหมือนฉันและฉันจะไม่แปลกใจหากการเปลี่ยนแปลงนี้ในอนาคต
let attrString = NSAttributedString(string: " Attributed Strings")
myString.append(attrString)

ต่อไปเราก็จะเลือก "สตริง" คำซึ่งเริ่มต้นที่ดัชนีและมีความยาวของ17 7แจ้งให้ทราบว่านี่เป็นและไม่ได้เป็นสวิฟท์NSRange Range(ดูคำตอบนี้สำหรับข้อมูลเพิ่มเติมเกี่ยวกับช่วง) addAttributeวิธีให้เราใส่ชื่อคีย์แอตทริบิวต์ในจุดแรกค่าแอตทริบิวต์ในจุดที่สองและช่วงในจุดที่สาม
var myRange = NSRange(location: 17, length: 7) // range starting at location 17 with a lenth of 7: "Strings"
myString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: myRange)

สุดท้ายเรามาเพิ่มสีพื้นหลัง เพื่อความหลากหลายลองใช้addAttributesวิธีการ (หมายเหตุs) ฉันสามารถเพิ่มคุณสมบัติหลายรายการพร้อมกันด้วยวิธีนี้ แต่ฉันจะเพิ่มอีกครั้ง
myRange = NSRange(location: 3, length: 17)
let anotherAttribute = [ NSAttributedString.Key.backgroundColor: UIColor.yellow ]
myString.addAttributes(anotherAttribute, range: myRange)

โปรดสังเกตว่าคุณลักษณะบางอย่างอาจทับซ้อนกันในบางสถานที่ การเพิ่มคุณสมบัติไม่ได้เขียนทับแอตทริบิวต์ที่มีอยู่แล้ว
ที่เกี่ยวข้อง
อ่านเพิ่มเติม
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue | NSUnderlineStyle.PatternDot.rawValue