![enter image description here](https://istack.dev59.com/QxiAo.webp)
本答案已更新至Swift 4.2。
快速参考
创建和设置富文本字符串的一般形式如下。您可以在下面找到其他常见选项。
let myString = "Swift Attributed String"
let myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
myLabel.attributedText = myAttrString
![Text Color](https://istack.dev59.com/XDDo6.webp)
let myAttribute = [ NSAttributedString.Key.foregroundColor: UIColor.blue ]
![Background Color](https://istack.dev59.com/HDzNA.webp)
let myAttribute = [ NSAttributedString.Key.backgroundColor: UIColor.yellow ]
![Font](https://istack.dev59.com/u9bI2.webp)
let myAttribute = [ NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 18.0)! ]
![enter image description here](https://istack.dev59.com/P51d9.webp)
let myAttribute = [ NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue ]
![enter image description here](https://istack.dev59.com/hguC7.webp)
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
还有许多其他属性。请参见 this link 了解更多信息。您甚至可以创建自己的自定义属性,例如:
key name: NSAttributedString.Key.myName
, value: some Type.
if you make an extension:
extension NSAttributedString.Key {
static let myName = NSAttributedString.Key(rawValue: "myCustomAttributeKey")
}
在Swift中创建属性
您可以像声明任何其他字典一样声明属性。
let singleAttribute1 = [ NSAttributedString.Key.foregroundColor: UIColor.green ]
let singleAttribute2 = [ NSAttributedString.Key.backgroundColor: UIColor.yellow ]
let singleAttribute3 = [ NSAttributedString.Key.underlineStyle: NSUnderlineStyle.double.rawValue ]
let multipleAttributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.foregroundColor: UIColor.green,
NSAttributedString.Key.backgroundColor: UIColor.yellow,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.double.rawValue ]
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
。以下是一些初始化方法:
let attrString1 = NSAttributedString(string: "Hello.")
let attrString2 = NSAttributedString(string: "Hello.", attributes: [NSAttributedString.Key.myName: "A value"])
let myAttributes1 = [ NSAttributedString.Key.foregroundColor: UIColor.green ]
let attrString3 = NSAttributedString(string: "Hello.", attributes: myAttributes1)
如果您需要稍后更改属性或字符串内容,则应使用
NSMutableAttributedString
。声明非常相似:
let mutableAttrString1 = NSMutableAttributedString()
let mutableAttrString2 = NSMutableAttributedString(string: "Hello.")
let mutableAttrString3 = NSMutableAttributedString(string: "Hello.", attributes: [NSAttributedString.Key.myName: "A value"])
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
。
这是结果:
![enter image description here](https://istack.dev59.com/Q7b6z.webp)
然后添加另一个没有设置任何属性的 attributed string。(注意,即使我在上面使用了 let 来声明 myString,我仍然可以修改它,因为它是 NSMutableAttributedString。这似乎与 Swift 不太相符,如果将来发生变化,我不会感到惊讶。当那种情况发生时,请给我留言。)
let attrString = NSAttributedString(string: " Attributed Strings")
myString.append(attrString)
![enter image description here](https://istack.dev59.com/IyiLM.webp)
接下来我们只需选择从索引
17
开始,长度为
7
的“Strings”单词。请注意,这是一个
NSRange
而不是Swift
Range
。(有关范围的更多信息,请参见
this answer。)
addAttribute
方法使我们能够将属性键名放在第一位,属性值放在第二位,范围放在第三位。
var myRange = NSRange(location: 17, length: 7)
myString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: myRange)
![enter image description here](https://istack.dev59.com/bXB7t.webp)
最后,让我们添加一个背景颜色。为了增加变化性,我们将使用addAttributes
方法(注意s
)。我可以通过这种方法一次添加多个属性,但是我只会再次添加一个。
myRange = NSRange(location: 3, length: 17)
let anotherAttribute = [ NSAttributedString.Key.backgroundColor: UIColor.yellow ]
myString.addAttributes(anotherAttribute, range: myRange)
![enter image description here](https://istack.dev59.com/x0x0L.webp)
注意,有些地方属性是重叠的。添加属性不会覆盖已经存在的属性。
相关
进一步阅读
NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue | NSUnderlineStyle.PatternDot.rawValue
。 - beebtextView.atrributedtText = myString
或者myLabel.attributedText = myString
放在你的回答开头。作为一个新手,我只是使用了myLabel.text
,没有想到需要阅读你的整个回答。attributedText
或text
,同时使用它们会没有意义?lineSpacing
示例,例如这个,因为它非常有用。append
和add
之间的区别令人困惑。appendAttributedString
类似于“字符串连接”。而addAttribute
是向您的字符串添加新属性。 - mfaani