Swift中的属性字符串
这是 我在Stack Overflow上写的答案的转贴 。
通过属性字符串,您可以使用自定义颜色,字体,下划线,阴影等来格式化文本范围。 尽管关于创建属性字符串的几个方面仍然感觉不太像Swift(例如NSRange),但它们仍为您设置文本格式提供了极大的灵活性。 在本文中,我们将探讨如何制作属性字符串。 它适用于初学者,但也可以为有经验的用户提供快速参考。
这里是创建属性字符串的基本代码。 将myAttribute
行替换为下面的其他选项之一,以对其进行更改。
//创建属性字符串
let myString =“快捷属性字符串”
让myAttribute = [NSAttributedString.Key.foregroundColor:UIColor.blue]
let myAttrString = NSAttributedString(string:myString,attribute:myAttribute)//在UILabel上设置属性文本
myLabel.attributedText = myAttrString
让myAttribute = [NSAttributedString.Key.foregroundColor:UIColor.blue]
让myAttribute = [NSAttributedString.Key.backgroundColor:UIColor.yellow]
让myAttribute = [NSAttributedString.Key.font:UIFont(name:“ Chalkduster”,size:18.0)! ]
让myAttribute = [NSAttributedString.Key.underlineStyle:NSUnderlineStyle.single.rawValue]
让myShadow = NSShadow()
myShadow.shadowBlurRadius = 3
myShadow.shadowOffset = CGSize(宽度:3,高度:3)
myShadow.shadowColor = UIColor.graylet myAttribute = [NSAttributedString.Key.shadow:myShadow]
这篇文章的其余部分为感兴趣的人提供了更多详细信息。
字符串属性只是[NSAttributedString.Key: Any]
形式的字典,其中NSAttributedString.Key
是属性的键名, Any
是某些Type的值。 该值可以是字体,颜色,整数或其他形式。 Swift中有许多预定义的标准属性。 例如:
- 项名称:
NSAttributedString.Key.font
,值:UIFont
- 项名称:
NSAttributedString.Key.foregroundColor
,值:UIColor
- 密钥名称:
NSAttributedString.Key.link
,值:NSURL
或NSString
还有很多。 有关更多信息,请参见此链接。 您甚至可以创建自己的自定义属性,例如:
- 项名称:
NSAttributedString.Key.myName
,值:一些类型。
如果您进行扩展:
扩展名NSAttributedString.Key {
静态让myName = NSAttributedString.Key(rawValue:“ myCustomAttributeKey”)
}
您可以像声明任何其他字典一样声明属性。
//一次声明一个属性
让singleAttribute1 = [NSAttributedString.Key.foregroundColor:UIColor.green]
让singleAttribute2 = [NSAttributedString.Key.backgroundColor:UIColor.yellow]
让singleAttribute3 = [NSAttributedString.Key.underlineStyle:NSUnderlineStyle.double.rawValue] //一次声明多个属性
让multipleAttributes:[NSAttributedString.Key:任何] = [
NSAttributedString.Key.foregroundColor:UIColor.green,
NSAttributedString.Key.backgroundColor:UIColor.yellow,
NSAttributedString.Key.underlineStyle:NSUnderlineStyle.double.rawValue] //自定义属性
让customAttribute = [NSAttributedString.Key.myName:“某些值”]
请注意 下划线样式值所需 的 rawValue
。
因为属性只是字典,所以您还可以通过创建一个空的字典,然后向其添加键值对来创建它们。 如果值将包含多种类型,则必须使用Any
作为类型。 这是上面的multiAttributes示例,以这种方式重新创建:
var multipleAttributes = [NSAttributedString.Key:任何]()
multipleAttributes [NSAttributedString.Key.foregroundColor] = UIColor.green
multipleAttributes [NSAttributedString.Key.backgroundColor] = UIColor.yellow
multipleAttributes [NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.double.rawValue
理解属性后,就可以创建属性字符串了。
初始化
有几种创建属性字符串的方法。 如果只需要一个只读字符串,则可以使用NSAttributedString
。 以下是一些初始化方法:
//仅使用字符串初始化
让attrString1 = NSAttributedString(string:“ Hello。”)//用字符串和内联属性初始化
让attrString2 = NSAttributedString(string:“ Hello。”,属性:[NSAttributedString.Key.myName:“ A value”])//用字符串初始化并单独声明属性
让myAttributes1 = [NSAttributedString.Key.foregroundColor:UIColor.green]
让attrString3 = NSAttributedString(string:“ Hello。”,attributes:myAttributes1)
如果以后需要更改属性或字符串内容,则应使用NSMutableAttributedString
。 声明非常相似:
//创建一个空白的属性字符串
让mutableAttrString1 = NSMutableAttributedString()//仅使用字符串初始化
let mutableAttrString2 = NSMutableAttributedString(string:“ Hello。”)//用字符串和内联属性初始化
让mutableAttrString3 = NSMutableAttributedString(string:“ Hello。”,attributes:[NSAttributedString.Key.myName:“ A value”])//用字符串初始化并单独声明属性
让myAttributes2 = [NSAttributedString.Key.foregroundColor:UIColor.green]
让mutableAttrString4 = NSMutableAttributedString(string:“ Hello。”,attributes:myAttributes2)
例如,让我们在这篇文章的顶部创建属性字符串。
首先创建一个具有新字体属性的NSMutableAttributedString
。
让myAttribute = [NSAttributedString.Key.font:UIFont(name:“ Chalkduster”,size:18.0)! ]
让myString = NSMutableAttributedString(string:“ Swift”,attribute:myAttribute)
如果您正在研究,请将属性字符串设置为UITextView
(或UILabel
),如下所示:
textView.attributedText = myString
您不使用textView.text
。
结果如下:
然后附加另一个未设置任何属性的属性字符串。 (请注意,即使我使用let
在上面声明myString
,我仍然可以对其进行修改,因为它是NSMutableAttributedString
。对于我来说,这似乎并不像Swift一样,如果将来发生更改,我也不会感到惊讶。发生。)
让attrString = NSAttributedString(string:“属性字符串”)
myString.append(attrString)
接下来,我们仅选择“字符串”一词,该词从索引17
开始,长度为7
。 注意,这是一个NSRange
而不是Swift Range
。 (有关范围的更多信息,请参addAttribute
答案。) addAttribute
方法使我们可以将属性键名称放在第一个位置,将属性值放在第二个位置,并将范围放在第三个位置。
var myRange = NSRange(位置:17,长度:7)//范围从位置17开始,长度为7:
myString.addAttribute(NSAttributedString.Key.foregroundColor,值:UIColor.red,范围:myRange)
最后,让我们添加背景色。 为了多样化,让我们使用addAttributes
方法(注意)。 我可以使用此方法一次添加多个属性,但我只会再次添加一个。
myRange = NSRange(位置:3,长度:17)
让anotherAttribute = [NSAttributedString.Key.backgroundColor:UIColor.yellow]
myString.addAttributes(anotherAttribute,范围:myRange)
请注意,属性在某些地方重叠。 添加属性不会覆盖已经存在的属性。
- 如何更改
NSMutableAttributedString
的文本但保留属性
- 如何从拍子位置检索属性
- 属性字符串编程指南(非常有用,但不幸的是,仅在Objective-C中)