NSAttributedString

字符属性
   
字符属性可以应用于 attributed string 的文本中。
   
NSString *const NSFontAttributeName;(字体)
   
NSString *const NSParagraphStyleAttributeName;(段落)
   
NSString *const NSForegroundColorAttributeName;(字体颜色)
   
NSString *const NSBackgroundColorAttributeName;(字体背景色)
   
NSString *const NSLigatureAttributeName;(连字符)
   
NSString *const NSKernAttributeName;(字间距)
   
NSString *const NSStrikethroughStyleAttributeName;(删除线)
   
NSString *const NSUnderlineStyleAttributeName;(下划线)
   
NSString *const NSStrokeColorAttributeName;(边线颜色)
   
NSString *const NSStrokeWidthAttributeName;(边线宽度)
   
NSString *const NSShadowAttributeName;(阴影)
   
NSString *const NSVerticalGlyphFormAttributeName;(横竖排版)
  
常量
   
1> NSFontAttributeName(字体)
   
该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,
则默认为12-point Helvetica(Neue)。
   
2> NSParagraphStyleAttributeName(段落)
   
该属性所对应的值是一个 NSParagraphStyle 对象。该属性在一段文本上应用多个属性。如果不指定该属性,
则默认为 NSParagraphStyle 的defaultParagraphStyle 方法返回的默认段落属性。
   
3> NSForegroundColorAttributeName(字体颜色)
   
该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。
   
4> NSBackgroundColorAttributeName(字体背景色)
   
该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的背景颜色。如果不指定该属性,则默认无背景色。
   
5> NSLigatureAttributeName(连字符)
   
该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。
0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。
   
6> NSKernAttributeName(字间距)
   
该属性所对应的值是一个 NSNumber 对象(整数)。字母紧排指定了用于调整字距的像素点数。字母紧排的效果依赖于字体。
值为 0 表示不使用字母紧排。默认值为0。
   
7> NSStrikethroughStyleAttributeName(删除线)
   
该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上删除线,该值参考“Underline Style Attributes”。
默认值是NSUnderlineStyleNone。
   
8> NSUnderlineStyleAttributeName(下划线)
   
该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上下划线,该值参考“Underline Style Attributes”。
默认值是NSUnderlineStyleNone。
   
9> NSStrokeColorAttributeName(边线颜色)
   
该属性所对应的值是一个 UIColor 对象。如果该属性不指定(默认),则等同于 NSForegroundColorAttributeName。
否则,指定为删除线或下划线颜色。更多细节见“Drawing attributedstrings that are both filled and stroked”。
   
10> NSStrokeWidthAttributeName(边线宽度)
   
该属性所对应的值是一个 NSNumber 对象(小数)。该值改变描边宽度(相对于字体size 的百分比)。
默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。
   
11> NSShadowAttributeName(阴影)
   
该属性所对应的值是一个 NSShadow 对象。默认为 nil。
   
12> NSVerticalGlyphFormAttributeName(横竖排版)
   
该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。
在 iOS 中,总是使用横排文本,0 以外的值都未定义。

由于iOS7新出的NSTextStorge是NSMutableAttributedString的子类,所以要用好NSTextStorage,首先要学好NSMutableAttributedString和NSAttributedString。

按个人的理解,NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。

因为是初步使用,所以基本上都是对照着文档上的指导和介绍的方法来写Demo。

首先是两种类的初始化方法(基本上是相似的):

[cpp]    view plain copy 在CODE上查看代码片 派生到我的代码片

  1.  1 // initWithString:  
     2 NSAttributedString *attributedString_str = [[NSAttributedString alloc] initWithString:@"attributedString"];  
     3 NSLog(@"%@", attributedString_str);  
     4 // textView.attributedText = attributedString_str;  
     5   
     6   
     7 // initWithAttributedString:  
     8 NSAttributedString *attributedString_atts = [[NSAttributedString alloc] initWithAttributedString:attributedString_str];  
     9 NSLog(@"%@", attributedString_atts);  
    10 // textView.attributedText = attributedString_atts;  
    11   
    12   
    13 // initWithString:attributes:  
    14 UIColor *backgroundColor = [UIColor blackColor];  
    15 NSNumber *baseLineOffset = [NSNumber numberWithFloat:20.0];  
    16 UIColor *foregroundColor = [UIColor whiteColor];  
    17 NSNumber *kern = [NSNumber numberWithFloat:5.0];  
    18 NSNumber *ligature = [NSNumber numberWithFloat:3.0];  
    19 NSURL *linkURL = [NSURL URLWithString:@"http://www.baidu.com"];  
    20 NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];  
    21 NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor,  
    22                            NSBackgroundColorAttributeName: backgroundColor,  
    23                            NSBaselineOffsetAttributeName: baseLineOffset,  
    24                            NSKernAttributeName: kern,  
    25                            NSLigatureAttributeName: ligature,  
    26                            NSLinkAttributeName: linkURL,  
    27                            NSUnderlineStyleAttributeName: underline  
    28                            };  
    29 NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic];  
    30 NSLog(@"%@", attributedString_str_atts);  
    31 // textView.attributedText = attributedString_str_atts;  
    32   
    33   
    34 // initWithFileURL:options:documentAttributes:error:  
    35 NSURL *fileURL = nil;  
    36 fileURL = [[NSBundle mainBundle] URLForResource:@"Dynamic Coloring" withExtension:@"rtf"];  
    37 NSAttributedString *attributedString_fileURL = [[NSAttributedString alloc] initWithFileURL:fileURL options:@{} documentAttributes:nil error:nil];  
    38 NSLog(@"%@", attributedString_fileURL);  
    39 // textView.attributedText = attributedString_fileURL;  
    40   
    41   
    42 // initWithData:options:documentAttributes:error:  
    43 fileURL = nil;  
    44 fileURL = [[NSBundle mainBundle] URLForResource:@"View Layout" withExtension:@"rtf"];  
    45 NSData *data = [[NSData alloc] initWithContentsOfURL:fileURL];  
    46 NSAttributedString *attributedString_data = [[NSAttributedString alloc] initWithData:data options:@{} documentAttributes:nil error:nil];  
    47 NSLog(@"%@", attributedString_data);  
    48 // textView.attributedText = attributedString_data;  
    49   
    50   
    51 // initWithAttributedString:  
    52 NSMutableAttributedString *mutableAttributedString_attrs = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString_fileURL];

非常简单。

由于NSAttributedString的属性以字典的形式记录,所以要弄清楚其中一些属性对应的键值:


说说几个自己使用上或者了解作用的。 

NSBackgroundColorAttributeName:文字背景的颜色。

NSBaselineOffsetAttributeName:设置行距。

NSFontAttributeName:字体的样式,必须设置NSFont作为Value,NSFont在iOS中不能使用,这个属性的设置上我还不会。

NSForegroundColorAttributeName:文字颜色。

NSUnderlineStyleAttributeName:为文字添加下划线。必须设置NSNumber对象为Value。如:

[cpp]    view plain copy 在CODE上查看代码片 派生到我的代码片

  1. NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];  

以上attribute在NSAttributedString和NSMutableAttributedString对象创建时可以设定,不同的是NSAttributedString对象在创建成功后其属性便不可改变,而NSMutableAttributedString的属性是可以改变的。如下所示:

[cpp]    view plain copy 在CODE上查看代码片 派生到我的代码片

  1.  1 // Change NSMutableAttributedString  
     2 [mutableAttributedString_attrs beginEditing];  
     3 /* 
     4 // addAttributes:range: 
     5 [mutableAttributedString_attrs addAttributes:@{NSLinkAttributeName: @"www.baidu.com", 
     6                                                NSBackgroundColorAttributeName: [UIColor greenColor], 
     7                                                NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleDouble] 
     8                                                } 
     9                                        range:NSMakeRange(0, [attributedString_fileURL length])]; */  
    10 // addAttribute:value:range:  
    11 [mutableAttributedString_attrs addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleThick] range:NSMakeRange(0, 10)];  
    12 [mutableAttributedString_attrs addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:20.0] range:NSMakeRange(20, 100)];  
    13 [mutableAttributedString_attrs endEditing];  
    14 NSLog(@"%@", mutableAttributedString_attrs);  
    15

在修改文字属性的开头和结尾要分别调用beginEditing和endEditing方法,这些方法可以在文字属性发生变化时发送消息给事件监听者。

可以为某个范围内的文字单个地添加属性key-value对,也可以添加一个属性字典。 
注意到在控制台输出NSAttributedString或NSMutableAttributedString时,输出的不仅是字符内容,还有对应的属性值:

[cpp]    view plain copy 在CODE上查看代码片 派生到我的代码片

  1. 1 2013-08-11 16:40:44.737 AttributedString_i7_Demo[43468:a0b] http://www.baidu.com{  
    2     NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";  
    3     NSBaselineOffset = 20;  
    4     NSColor = "UIDeviceWhiteColorSpace 1 1";  
    5     NSKern = 5;  
    6     NSLigature = 3;  
    7     NSLink = "http://www.baidu.com";  
    8     NSUnderline = 1;  
    9 }

也可以获取某一段文字的属性:

[cpp]    view plain copy 在CODE上查看代码片 派生到我的代码片

  1. 1 // get attribute  
    2 NSRange range = NSMakeRange(0, mutableAttributedString_attrs.length);  
    3 // attributesAtIndex:effectiveRange:  
    4 NSDictionary *dic = [mutableAttributedString_attrs attributesAtIndex:0 effectiveRange:&range];  
    5 NSLog(@"%@", [dic objectForKey:NSFontAttributeName]);


如果要将NSMutableAttributedString对象赋值给NSAttributedString时,要使用copy方法:

[cpp]    view plain copy 在CODE上查看代码片 派生到我的代码片

  1. 1 textView.attributedText = [mutableAttributedString_attrs copy];

    以上是对NSAttributedString和NSMutableAttributedString最基本的使用(主要还是NSAttributedString),为了强化作为NSMutableAttributedString的子类NSTextStorage处理文本的能力,明显在这两个类中增加了许多新的成员,如NSTextAttachment,在这里没有用上,必须继续深入学习。 

原文地址:https://www.cnblogs.com/conanwin/p/4779216.html