NSAttributedString 管理character strings,属性的集合,可以设置单个character的或string中的部分字符的属性
characters和attributes之间的联系称之为attributed string
NSAttributedString:read only attributed string
NSMutableAttributedString: 即可读又可写的字符串
一个attributed string用name 来识别attribute,用NSDictionary对象来存储value和attributed name.可以分配任何attributed name和value pairs应用到选定范围的characters上,
in ios: standard attributed keys定义在:NSAttributedString UIKit Additions reference
in os x:standard attributed keys定义在:NSAttributedString application kit Additions reference
NSAttributedString并不是一个NSString的一个子类,它包含一个应用attribute的NSString对象,NSAttributedString采纳了NSCopying和NSMutableCopying协议,这使得转换NSAttributedString到另外一个变得容易
一个属性字符串通过名字来识别属性,用一个NSDictionary对象来存储attribute的值,text font用名字NSFontAttributedName对应的NSFont对象来存储,
可以用不同的方式来创建NSAttributedString
可以用initWithString:,initWithString:attributes:,initWithAttributedString:,这些方法用你提供的数据来创建一个attributed string
NSFont *font=[NSFont fontWithName:@"Palatino-Roman",size:14.0];
NSDictionary *attrDictionary =[NSDictionary dictionaryWithObject:font forKey:NSFontAttributedName];
NSAttributedString *attrString = [[NSAttributedString alloc]initWIthString:@"string1" attributes:attrDictionary];
被分配给attributed string的 attributes value变成string的property,是不能被别的对象更改的
可以用NSMutableAttributedString的方法:setAttributeds:range:来设定在某一个范围内的属性值
用RTF格式和RTFD格式的数据来创建一个attributed string
NSData *rtfData = ...; // assume rtfData is an NSData object containing valid RTF data |
NSDictionary *docAttributes; |
NSSize paperSize; |
|
NSAttributedString *attrString; |
|
if ((attrString = [[NSAttributedString alloc] |
initWithRTF: rtfData documentAttributes: &docAttributes])) { |
|
NSValue *value = [docAttrs objectForKey:@"PaperSize"]; |
paperSize = [value sizeValue]; |
// implementation continues... |
对不可改变的attributed string,当创建attributedString时就必须指明他的所有属性
在Objective-c中用方法:initWithString:attributes: 参数中用一个NSDictionary object来存储attribute name和value
或者用initWithString:不指明任何属性
从一个Attributed string中取出attribute value的方法
-
rulerAttributesInRange:
中间两个方法返回在attributedName处的属性值return the value of a single named attribute.
最后两个方法是Application Kit’s extensions , return attributes defined to apply only to characters or to whole paragraphs, respectively.
在attributedstring中的每一个字符都有他自己的 collection of attributes,然而通常一系列字符的属性和value是相同的
character attributes
key: NSFontAttributeName value:UIFont对象,设定character 的字体
key:NSParagraphStyleAttributeName value :NSParagraphStyle对象 Use this attribute to apply multiple attributes to a range of text.
key:NSForegroundColorAttributeName value: UIColor Use this attribute to specify the color of the text during rendering
key:NSBackgroundColorAttributedName value:UIColor Use this attribute to specify the color of the background area behind the text
key:NSUnderlineStyleAttributeName value:NSNumber This value indicates whether the text is underlined
key:NSStrokeColorAttributeName value:UIColor If it is not defined (which is the case by default), it is assumed to be the same as the value of NSForegroundColorAttributeName
; otherwise, it describes the outline color.
key:NSStrokeWidthAttributedName value:NSNumber Specify positive values to change the stroke width alone. Specify negative values to stroke and fill the text