NSAttributedString 详解


转自:http://blog.csdn.net/zhangao0086/article/details/7616385

http://www.cnblogs.com/whyandinside/archive/2013/12/27/3493475.html

NSAttributedString可以让我们使一个字符串显示的多样化,但是目前到iOS 5为止,好像对它支持的不是很好,因为显示起来不太方便(至少没有在OS X上方便)。

首先导入CoreText.framework,并在需要使用的文件中导入:

#import<CoreText/CoreText.h>

创建一个NSMutableAttributedString:

  1. NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"]   
  2.                                               autorelease];  
非常常规的创建方式,接下来我们给它配置属性:

  1. //把this的字体颜色变为红色  
  2. [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName  
  3.                     value:(id)[UIColor redColor].CGColor   
  4.                     range:NSMakeRange(0, 4)];  
  5. //把is变为黄色  
  6. [attriString addAttribute:(NSString *)kCTForegroundColorAttributeName  
  7.                     value:(id)[UIColor yellowColor].CGColor   
  8.                     range:NSMakeRange(5, 2)];  
  9. //改变this的字体,value必须是一个CTFontRef  
  10. [attriString addAttribute:(NSString *)kCTFontAttributeName  
  11.                     value:(id)CTFontCreateWithName((CFStringRef)[UIFont boldSystemFontOfSize:14].fontName,  
  12.                                                    14,   
  13.                                                    NULL)  
  14.                     range:NSMakeRange(0, 4)];  
  15. //给this加上下划线,value可以在指定的枚举中选择  
  16. [attriString addAttribute:(NSString *)kCTUnderlineStyleAttributeName  
  17.                     value:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble]  
  18.                     range:NSMakeRange(0, 4)];  
  19. return attriString;  

这样就算是配置好了,但是我们可以发现NSAttributedString继承于NSObject,并且不支持任何draw的方法,那我们就只能自己draw了。写一个UIView的子类(假设命名为TView),在initWithFrame中把背景色设为透明(self.backgroundColor = [UIColor clearColor]),然后在重写drawRect方法:

  1. -(void)drawRect:(CGRect)rect{  
  2.     [super drawRect:rect];  
  3.       
  4.     NSAttributedString *attriString = getAttributedString();  
  5.       
  6.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  7.     CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));  
  8.       
  9.     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attriString);  
  10.     CGMutablePathRef path = CGPathCreateMutable();  
  11.     CGPathAddRect(path, NULL, rect);  
  12.       
  13.     CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);  
  14.     CFRelease(path);  
  15.     CFRelease(framesetter);  
  16.       
  17.     CTFrameDraw(frame, ctx);  
  18.     CFRelease(frame);  
  19. }  

在代码中我们调整了CTM(current transformation matrix),这是因为Quartz 2D的坐标系统不同,比如(10, 10)到(20, 20)的直线坐标:

 

坐标类似于数学中的坐标,可以先不调整CTM,看它是什么样子的,下面两种调整方法是完全一样的:

  1. CGContextConcatCTM(ctx, CGAffineTransformScale(CGAffineTransformMakeTranslation(0, rect.size.height), 1.f, -1.f));  
==
  1. CGContextTranslateCTM(ctx, 0, rect.size.height);  
  2. CGContextScaleCTM(ctx, 1, -1);  

CTFramesetter是CTFrame的创建工厂,NSAttributedString需要通过CTFrame绘制到界面上,得到CTFramesetter后,创建path(绘制路径),然后得到CTFrame,最后通过CTFrameDraw方法绘制到界面上

如果想要计算NSAttributedString所要的size,就需要用到这个API:

CTFramesetterSuggestFrameSizeWithConstraints,用NSString的sizeWithFont算多行时会算不准的,因为在CoreText里,行间距也是你来控制的。

设置行间距和换行模式都是设置一个属性:kCTParagraphStyleAttributeName,这个属性里面又分为很多子

属性,其中就包括

  • kCTLineBreakByCharWrapping
  • kCTParagraphStyleSpecifierLineSpacingAdjustment
设置如下:

  1. //段落  
  2.     //line break  
  3. CTParagraphStyleSetting lineBreakMode;  
  4. CTLineBreakMode lineBreak = kCTLineBreakByCharWrapping; //换行模式  
  5. lineBreakMode.spec = kCTParagraphStyleSpecifierLineBreakMode;  
  6. lineBreakMode.value = &lineBreak;  
  7. lineBreakMode.valueSize = sizeof(CTLineBreakMode);  
  8.     //行间距  
  9. CTParagraphStyleSetting LineSpacing;  
  10. CGFloat spacing = 4.0;  //指定间距  
  11. LineSpacing.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;  
  12. LineSpacing.value = &spacing;  
  13. LineSpacing.valueSize = sizeof(CGFloat);  
  14.   
  15. CTParagraphStyleSetting settings[] = {lineBreakMode,LineSpacing};  
  16. CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, 2);   //第二个参数为settings的长度  
  17. [attributedString addAttribute:(NSString *)kCTParagraphStyleAttributeName  
  18.                          value:(id)paragraphStyle  
  19.                          range:NSMakeRange(0, attributedString.length)];  

-----------------------------------------猥琐的分界线-----------------------------------------

这并不是唯一的方法,还有另一种替代方案:

  1. CATextLayer *textLayer = [CATextLayer layer];  
  2. textLayer.string = getAttributedString();  
  3. textLayer.frame = CGRectMake(0, CGRectGetMaxY(view.frame), 200, 200);  
  4. [self.view.layer addSublayer:textLayer];  
CATextLayer可以直接支持NSAttributedString!

-----------------------------------------猥琐的分界线-----------------------------------------

效果图:


源码地址

-----------------------------------------猥琐的分界线-----------------------------------------
目前发现有一个问题,好像中文全都会被加粗,设置不加粗的字体也没用,应该是CoreText的bug,已经上报给了apple了。

----------------------------------------- 对于cythb兄提出的问题 -----------------------------------------
首先表示感谢关注

原文中确有描述不适当的地方,比如:The upper-left corner of the context is (0, 0) 。实际上Quartz2D的坐标系统确实在左下角,只是有一些技术在设置它们的graphics context时使用了不同于Quartz的默认坐标系统。相对于Quartz来说,这些坐标系统是修改的坐标系统(modified coordinate system)。


UPDATED

在iOS6之后,创建一个AttributedString变成了一件轻松的事情, <CoreText/CoreText.h> 已经不需要导入了。如果我要设置字体的颜色,可以直接这样:

  1. [textAttr addAttribute:NSForegroundColorAttributeName  
  2.                        value:[UIColor redColor]  
  3.                        range:NSMakeRange(0, text.length)];  


An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string.

这句话就是对这个类的一个最简明扼要的概括。NSAttributedString管理一个字符串,以及与该字符串中的单个字符或某些范围的字符串相关的属性。比如这个字符串“北京天安门”,“我”跟其他字符的颜色不一样,而“北京”与其他的字体和大小不一样,等等。NSAttributedString就是用来存储这些信息的,具体实现时,NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用来保存各个子串/字符的属性。

Create Attributed String

有3种方法创建Attributed String。

1. 使用initWithString:, initWithString:attributes:, 或者 initWithAttributedString: ,下面是一个实例代码:

1
2
3
4
5
NSFont  *font = [ NSFont  fontWithName:@ "Palatino-Roman"  size:14.0];
NSDictionary  *attrsDictionary = [ NSDictionary  dictionaryWithObject:font
                                     forKey: NSFontAttributeName ];
NSAttributedString  *attrString = [[ NSAttributedString  alloc] initWithString:@ "strigil"
             attributes:attrsDictionary];

 可以看到上面创建的整个字符串关联了Font属性。如果希望只是对某一范围的字符串施加某个属性应该使用NSMutableAttributedString的 setAttributes:range:方法。这里例子是使用了Font属性,在Appkit中特殊定义了若干属性,这些属性被用于Core Text中。其他的属性包括前景色、背景色、是否有shadow等,具体可见本文

2. 使用initWithRTF:documentAttributes:, initWithRTFD:documentAttributes:, and initWithRTFDFileWrapper:documentAttributes:从rich text (RTF) 或者 rich text with attachments (RTFD) 数据中创建。

复制代码
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...
复制代码

3. 使用initWithHTML:documentAttributes: 和 initWithHTML:baseURL:documentAttributes:从HTML数据中创建。有线程安全问题,使用时需要注意。

对RTF和HTML的支持都是AppKit对NSAttributedString的扩展。

Accessing Attributes

从上面对这个类的介绍可以知道,如果我们要访问某个子串/字符的属性,需要提供子串的位置和属性的名字,而如果不提供属性名字,那就把所有属性都返回。下面就是其对应的APIs:

1
2
3
4
5
6
attributesAtIndex:effectiveRange:
attributesAtIndex:longestEffectiveRange:inRange:
attribute:atIndex:effectiveRange:
attribute:atIndex:longestEffectiveRange:inRange:
fontAttributesInRange:
rulerAttributesInRange:

 fontAttributesInRange: 和 rulerAttributesInRange: 是由AppKit扩展的属性。

The first four methods also return by reference the effective range and the longest effective range of the attributes. These ranges allow you to determine the extent of attributes. Conceptually, each character in an attributed string has its own collection of attributes; however, it’s often useful to know when the attributes and values are the same over a series of characters. This allows a routine to progress through an attributed string in chunks larger than a single character. In retrieving the effective range, an attributed string simply looks up information in its attribute mapping, essentially the dictionary of attributes that apply at the index requested. In retrieving the longest effective range, the attributed string continues checking characters past this basic range as long as the attribute values are the same. This extra comparison increases the execution time for these methods but guarantees a precise maximal range for the attributes requested.

Methods that return an effective range by reference are not guaranteed to return the maximal range to which the attribute(s) apply; they are merely guaranteed to return some range over which they apply. In practice they will return whatever range is readily available from the attributed string's internal storage mechanisms, which may depend on the implementation and on the precise history of modifications to the attributed string.

那些用reference返回有效范围的方法并不保证一定返回attribute应用的最大范围。它们只保证返回那些attribute有效的一些范围。实际上,它们只是返回attributed string中容易返回的那些信息,是否容易与内部实现,已经对attributed string的精确修改历史有关。

Methods that return a longest effective range by reference, on the other hand, are guaranteed to return the longest range containing the specified index to which the attribute(s) in question apply (constrained by the value of the argument passed in forinRange:). For efficiency, it is important that the inRange: argument should be as small as appropriate for the range of interest to the client.

那些返回最长有效范围的方法时能够保证返回制定attribute有效的最长的range的。为了效率,inRange: 参数应该尽量小,能满足客户需要就好。

When you iterate over an attributed string by attribute ranges, either sort of method may be appropriate depending on the situation. If there is some processing to be done for each range, and you know that the full range for a given attribute is going to have to be handled eventually, it may be more efficient to use the longest-effective-range variant, so as not to have to handle the range in pieces. However, you should use the longest-effective-range methods with caution, because the longest effective range could be quite long—potentially the entire length of the document, if the inRange: argument is not constrained.

 

Changing an Attributed String

NSMutableAttributedString提供若干方法,即可以修改字符串,又可以修改字符串的属性。经过多次修改后,有些信息可能变的不一致了,为了让信息保持一致,可以使用下面的方法:

1
2
3
4
5
6
fixAttributesInRange:
fixAttachmentAttributeInRange:
fixFontAttributeInRange:
fixParagraphStyleAttributeInRange:
beginEditing
endEditing

 

这些方法都是AppKit的扩展功能。

 

Reference:

1. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html#//apple_ref/doc/uid/10000036-BBCCGDBG 

2. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAttributedString_AppKitAdditions/Reference/Reference.html#//apple_ref/doc/uid/20000167-BAJJCCFC



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值