NSAttributedString

本文详细介绍了NSAttributedString的概念及使用方法,包括NSAttributedString和NSMutableAttributedString的区别、如何创建和获取NSAttributedString中的属性等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的方法

  • attributesAtIndex:effectiveRange:

  • attributesAtIndex:longestEffectiveRange:inRange:

  • attribute:atIndex:effectiveRange:

  • attribute:atIndex:longestEffectiveRange:inRange:

  • fontAttributesInRange:

  • rulerAttributesInRange:

前两个方法返回在index处的字符串的所有的属性   return all attributes at a given index

中间两个方法返回在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  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值