此答案已针对 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]
, where NSAttributedString.Key
是属性的键名,Any
是某种类型的值。该值可以是字体、颜色、整数或其他内容。 Swift 中有许多已经预定义的标准属性。例如:
- 按键名称:
NSAttributedString.Key.font
,值:aUIFont
- 按键名称:
NSAttributedString.Key.foregroundColor
,值:aUIColor
- 按键名称:
NSAttributedString.Key.link
,值:一个NSURL
or NSString
还有很多其他的。看这个链接 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/Articles/standardAttributes.html了解更多。您甚至可以创建自己的自定义属性,例如:
-
按键名称:NSAttributedString.Key.myName
,值:某种类型。
如果你做了一个扩大 https://stackoverflow.com/q/33942483/3681880:
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 https://craiggrummitt.wordpress.com/2016/09/16/any-vs-anyobject-vs-nsobject-in-swift-3/作为类型。这里是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
(or UILabel
) 像这样:
textView.attributedText = myString
You don't use textView.text
.
结果如下:
然后附加另一个未设置任何属性的属性字符串。 (请注意,即使我使用let
声明myString
上面,我仍然可以修改它,因为它是NSMutableAttributedString
。这对我来说似乎不太像,如果将来这种情况发生变化,我不会感到惊讶。发生这种情况时请给我留言。)
let attrString = NSAttributedString(string: " Attributed Strings")
myString.append(attrString)
接下来我们只选择“Strings”单词,它从索引开始17
并且长度为7
。请注意,这是一个NSRange
而不是斯威夫特Range
. (See 这个答案 https://stackoverflow.com/a/35193481/3681880有关范围的更多信息。)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)
请注意,属性在某些地方是重叠的。添加属性不会覆盖已有的属性。
Related
- 如何更改文本NSMutableAttributedString但保留属性 https://stackoverflow.com/a/34628596/3681880
进一步阅读
- 如何从点击位置检索属性 https://stackoverflow.com/a/32262426/3681880
-
属性字符串编程指南 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html(信息非常丰富,但不幸的是仅在 Objective-C 中)