此答案已针对 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是某个Type的值。该值可以是字体、颜色、整数或其他内容。 Swift 中有许多已经预定义的标准属性。例如:
- 键名:
NSAttributedString.Key.font,值:aUIFont
- 键名:
NSAttributedString.Key.foregroundColor,值:aUIColor
- 键名:
NSAttributedString.Key.link,值:NSURL 或 NSString
还有很多其他的。有关更多信息,请参阅this link。您甚至可以制作自己的自定义属性,例如:
在 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)
更改属性字符串
例如,让我们在这篇文章的顶部创建属性字符串。
首先创建一个带有新字体属性的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。这对我来说似乎很不Swift,如果将来这种情况发生变化,我不会感到惊讶。发生这种情况时给我留言。)
let attrString = NSAttributedString(string: " Attributed Strings")
myString.append(attrString)
接下来我们将只选择“字符串”字,它从索引17 开始,长度为7。请注意,这是 NSRange 而不是 Swift Range。 (有关 Ranges 的更多信息,请参阅 this answer。)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)
请注意,属性在某些地方是重叠的。添加属性不会覆盖已经存在的属性。
相关
进一步阅读