一、字符属性
字符属性:字符属性可以应用于NSAttributedString中
- (1)、NSString *const NSFontAttributeName;(字体)
- (2)、NSString *const NSParagraphStyleAttributeName;(段落)
- (3)、NSString *const NSForegroundColorAttributeName;(字体颜色)
- (4)、NSString *const NSBackgroundColorAttributeName;(字体背景色)
- (5)、NSString *const NSLigatureAttributeName;(连字符)
- (6)、NSString *const NSKernAttributeName;(字间距)
- (7)、NSString *const NSStrikethroughStyleAttributeName;(删除线)
- (8)、NSString *const NSUnderlineStyleAttributeName;(下划线)
- (9)、NSString *const NSStrokeColorAttributeName;(边线颜色)
- (10)、NSString *const NSStrokeWidthAttributeName;(边线宽度)
- (11)、NSString *const NSShadowAttributeName;(阴影)
- (12)、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对象(整数)。该值指定是否在文字上加上删除线,默认值是NSUnderlineStyleNone。改值参考“Underline Style Attributes” - (8)、NSUnderlineStyleAttributeName(下划线)
该属性所对应的值是一个NSNumber对象(整数)。该值指定是否在文字上加上下划线。,默认值是NSUnderlineStyleNone; - (9)、NSStrokeColorAttributeName(边线颜色)
该属性所对应的值是一个UIColor对象。该属性不指定(默认)则等同于NSForegroundColorAttributeName。否则,是定为删除线或下划线颜色。 - (10)、NSStrokeWidthAttributeName边线宽度
该属性所对应的值是一个NSNumber对象(小数)。该值改变描边宽度(相当于字体size的百分比)。默认为0,既不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。 - (11)、NSShadowAttributeName(阴影)
该属性所对应的值是一个NSShadow对象。默认为nil。 - (12)、NSVerticalGlyphFormAttributeName(横竖排版)
该属性所对应的值是一个NSNumber对象(整数)。0表示横排文本。1表示竖排文本。在iOS中,总是使用横排文本,0以外的值都未定义。
二、NSAttributeString管理字符串
NSAttributeString管理一个字符串,以及与该字符串中的单个字符或某些范围的字符串的相关的属性。
NSAttributedString就是用来存储这些信息的。具体实现时,NSAttributedString维护了一个NSString,用来保存最原始的字符串,另有一个NSDictionary用于保存个个子串/字符的属性。
有两种方法创建NSAttributedString
(1)、
- (instancetype)initWithString:(NSString *)str;
- (instancetype)initWithString:(NSString )str attributes:(NSDictionary )attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
例如:创建字符串关联Font属性
UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@“strigil" attributes:attrsDictionary];
可以看到上面创建的整个字符串关联了Font属性。如果希望只是对某一范围的字符串施加某个属性应该使用NSMutableAttributedString的setAttribute:range:方法。即: - (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
例如:创建不同颜色的label
NSString*string = @"如何使得Label里的字符有不同的颜色?";
NSRange range = [string rangeOfString: @"Label”];
NSMutableAttributedString*attribute = [[NSMutableAttributedString alloc] initWithString: string];
[attribute beginEditing];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor redColor]}range: range];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor greenColor]}range: NSMakeRange(0, range.location)];
[attribute addAttributes: @{NSForegroundColorAttributeName: [UIColor cyanColor]}range: NSMakeRange(range.location+ range.length, 5)];
[attribute endEditing];
(2)、
- (instancetype)initWithFileURL:(NSURL )url options:(NSDictionary )options documentAttributes:(NSDictionary )dict error:(NSError )error NS_AVAILABLE_IOS(7_0);
- (instancetype)initWithData:(NSData )data options:(NSDictionary )options documentAttributes:(NSDictionary )dict error:(NSError )error NS_AVAILABLE_IOS(7_0);
从HTML数据中创建 。有线程安全问题,使用时需注意。
例如:使用URL创建NSAttributedString
NSURL *html = [[NSBundle mainBundle] URLForResource: @"test" withExtension:@"html"];
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithFileURL:html options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
// 创建TextView,添加attributed str
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 320, 50)];
[textView setAttributedText:attrStr];
例如:使用Data创建NSAttributeString
NSString* strUrl = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"html"];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[strUrl dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
// 创建TextView,添加attributed str
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 320, 50)];
[textView setAttributedText:attributedString];