NSMutableAttributedString/NSAttr
//UILabel 设置NSAttributedString
NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];
[ps setAlignment:NSTextAlignmentCenter];
NSDictionary *attribs = @{NSFontAttributeName: [UIFont fontWithName:@"Microsoft Yahei" size:45],NSParagraphStyleAttributeName:ps};
NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:super.text
attributes:attribs];
super.attributedText = attributedText;
UITextView *lab = [LocalTexts objectAtIndex:j];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 50.0f;
paragraphStyle.maximumLineHeight = 50.0f;
paragraphStyle.minimumLineHeight = 50.0f;
NSString *string = lab.text;
NSDictionary *ats = @{NSFontAttributeName : [UIFont fontWithName:@"DIN Medium" size:16.0f],NSParagraphStyleAttributeName : paragraphStyle, };
lab.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];
//textview 设置行间距
NSString *const NSForegroundColorAttributeName;//值为UIColor,字体颜色,默认为黑色。
NSString *const NSBackgroundColorAttributeName;//值为UIColor,字体背景色,默认没有。
NSString *const NSLigatureAttributeName;//值为整型NSNumber,连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios 上不支持。
NSString *const NSKernAttributeName;//值为浮点数NSNumber,字距属性,默认值为0。
NSString *const NSStrikethroughStyleAttributeName;//值为整型NSNumber,可取值为
enum {
NSUnderlineStyleNone = 0×00,
NSUnderlineStyleSingle = 0×01,
};设置删除线。
NSString *const NSUnderlineStyleAttributeName;//同上。设置下划线。
NSString *const NSStrokeColorAttributeName;//值为UIColor,默认值为nil,设置的属性同ForegroundColor。
NSString *const NSStrokeWidthAttributeName;//值为浮点数NSNumber。设置比画的粗细。
NSString *const NSShadowAttributeName;//值为NSShadow,设置比画的阴影,默认值为nil。
NSString *const NSVerticalGlyphFormAttributeName;//值为整型NSNumber,0为水平排版的字,1为垂直排版的字。
// initWithString:
NSAttributedString *attributedString_str = [[NSAttributedString alloc] initWithString:@"attributedString"];
NSLog(@"%@", attributedString_str);
// textView.attributedText = attributedString_str;
// initWithAttributedString:
NSAttributedString *attributedString_atts = [[NSAttributedString alloc] initWithAttributedString:attributedString_str];
NSLog(@"%@", attributedString_atts);
// textView.attributedText = attributedString_atts;
// initWithString:attributes:
UIColor *backgroundColor = [UIColor blackColor];
NSNumber *baseLineOffset = [NSNumber numberWithFloat:20.0];
UIColor *foregroundColor = [UIColor whiteColor];
NSNumber *kern = [NSNumber numberWithFloat:5.0];
NSNumber *ligature = [NSNumber numberWithFloat:3.0];
NSURL *linkURL = [NSURL URLWithString:@"http://www.baidu.com"];
NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor,
NSBackgroundColorAttributeName: backgroundColor,
NSBaselineOffsetAttributeName: baseLineOffset,
NSKernAttributeName: kern,
NSLigatureAttributeName: ligature,
NSLinkAttributeName: linkURL,
NSUnderlineStyleAttributeName: underline
};
NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic];
NSLog(@"%@", attributedString_str_atts);
// textView.attributedText = attributedString_str_atts;
// initWithFileURL:options:documentAttributes:error:
NSURL *fileURL = nil;
fileURL = [[NSBundle mainBundle] URLForResource:@"Dynamic Coloring" withExtension:@"rtf"];
NSAttributedString *attributedString_fileURL = [[NSAttributedString alloc] initWithFileURL:fileURL options:@{} documentAttributes:nil error:nil];
NSLog(@"%@", attributedString_fileURL);
// textView.attributedText = attributedString_fileURL;
// initWithData:options:documentAttributes:error:
fileURL = nil;
fileURL = [[NSBundle mainBundle] URLForResource:@"View Layout" withExtension:@"rtf"];
NSData *data = [[NSData alloc] initWithContentsOfURL:fileURL];
NSAttributedString *attributedString_data = [[NSAttributedString alloc] initWithData:data options:@{} documentAttributes:nil error:nil];
NSLog(@"%@", attributedString_data);
// textView.attributedText = attributedString_data;
// initWithAttributedString:
NSMutableAttributedString *mutableAttributedString_attrs = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString_fileURL];
由于NSAttributedString的属性以字典的形式记录,所以要弄清楚其中一些属性对应的键值:
屏幕快照 2016-06-01 下午7.50.50.pngNSBackgroundColorAttributeName:文字背景的颜色。
NSBaselineOffsetAttributeName:设置行距。
NSFontAttributeName:字体的样式,必须设置NSFont作为Value,NSFont在iOS中不能使用,这个属性的设置上我还不会。
NSForegroundColorAttributeName:文字颜色。
NSUnderlineStyleAttributeName:为文字添加下划线。必须设置NSNumber对象为Value。如
NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
以上attribute在NSAttributedString和NSMutableAttributedString对象创建时可以设定,不同的是NSAttributedString对象在创建成功后其属性便不可改变,而NSMutableAttributedString的属性是可以改变的。如下所示:
[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/84484/fork)
// Change NSMutableAttributedString
[mutableAttributedString_attrs beginEditing];
/*
// addAttributes:range:
[mutableAttributedString_attrs addAttributes:@{NSLinkAttributeName: @"www.baidu.com",
NSBackgroundColorAttributeName: [UIColor greenColor],
NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleDouble]
}
range:NSMakeRange(0, [attributedString_fileURL length])]; */
// addAttribute:value:range:
[mutableAttributedString_attrs addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleThick] range:NSMakeRange(0, 10)];
[mutableAttributedString_attrs addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:20.0] range:NSMakeRange(20, 100)];
[mutableAttributedString_attrs endEditing];
NSLog(@"%@", mutableAttributedString_attrs);
在修改文字属性的开头和结尾要分别调用beginEditing和endEditing方法,这些方法可以在文字属性发生变化时发送消息给事件监听者。
可以为某个范围内的文字单个地添加属性key-value对,也可以添加一个属性字典。
注意到在控制台输出NSAttributedString或NSMutableAttributedString时,输出的不仅是字符内容,还有对应的属性值:
2016-06-1 16:40:44.737 AttributedString_i7_Demo[43468:a0b] http://www.baidu.com{
NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";
NSBaselineOffset = 20;
NSColor = "UIDeviceWhiteColorSpace 1 1";
NSKern = 5;
NSLigature = 3;
NSLink = "http://www.baidu.com";
NSUnderline = 1;
}
也可以获取某一段文字的属性:
// get attribute
NSRange range = NSMakeRange(0, mutableAttributedString_attrs.length);
// attributesAtIndex:effectiveRange:
NSDictionary *dic = [mutableAttributedString_attrs attributesAtIndex:0 effectiveRange:&range];
NSLog(@"%@", [dic objectForKey:NSFontAttributeName]);
如果要将NSMutableAttributedString对象赋值给NSAttributedString时,要使用copy方法:
textView.attributedText = [mutableAttributedString_attrs copy];
以上是对NSAttributedString和NSMutableAttributedString最基本的使用(主要还是NSAttributedString),为了强化作为NSMutableAttributedString的子类NSTextStorage处理文本的能力,明显在这两个类中增加了许多新的成员,如NSTextAttachment,在这里没有用上,必须继续深入学习。