设置某一段文字的颜色 NSForegroundColorAttributeName value: UIColor 对象
设置某一段文字的字体大小 NSFontAttributeName value:UIFont对象
设置某一段文字的下划线 NSUnderlineStyleAttributeName value: NSNmuber对象 设置下划线宽度
设置下划线的颜色 NSUnderlineColorAttributeName value: UIColor 对象
设置某一段文字的删除线 NSStrikethroughStyleAttributeName value:NSNumber对象 设置删除线的宽度
设置删除线的颜色 NSStrikethroughColorAttributeName value: UIColor对象
设置某一段文字的背景色 NSBackgroundColorAttributeName value: UIColor对象
设置某一段文字的宽度 NSStrokeWidthAttributeName value: NSNumber对象
设置字体间距 NSKernAttributeName value:NSNumber对象
字体倾斜 NSObliquenessAttributeName value:NSNumber
阴影偏移 NSShadowAttributeName value:shadow对象
设置文字的扁平化比例 NSExpansionAttributeName value: NSNumber对象
插入一段文字
- (void)insertAttributedString:(NSAttributedString*)attrString atIndex:(NSUInteger)loc;
替换一段文字
- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString*)attrString;
删除一段文字
- (void)deleteCharactersInRange:(NSRange)range;
删除某一段文字的富文本设置
- (void)removeAttribute:(NSAttributedStringKey)name range:(NSRange)range;
拼接一段字符串
- (void)appendAttributedString:(NSAttributedString*)attrString;
demo
/*设置某一段文字的颜色*/
[stringaddAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(1, 3)];
/*设置某一段文字的字体*/
[stringaddAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(1, 3)];
/*设置某一段文字的下划线*/
[stringaddAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:3] range:NSMakeRange(60, 3)];
/*设置下划线的颜色*/
[stringaddAttribute:NSUnderlineColorAttributeName value:[UIColor blackColor] range:NSMakeRange(1, 3)];
/*设置某一段文字的删除线*/
[stringaddAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:4] range:NSMakeRange(65, 3)];
/*设置删除线的颜色*/
[stringaddAttribute:NSStrikethroughColorAttributeName value:[UIColor blackColor] range:NSMakeRange(65, 3)];
/*设置某一段文字的背景色*/
[stringaddAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(1, 3)];
/*设置某一段文字的宽度*/
[stringaddAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:10] range:NSMakeRange(1, 3)];
/*设置字体间距*/
[stringaddAttribute:NSKernAttributeName value:[NSNumber numberWithInt:20] range:NSMakeRange(8, 3)];
[stringaddAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:1000] range:NSMakeRange(0, 1)];
/*字体倾斜*/
[stringaddAttribute:NSObliquenessAttributeName value:@(0.5) range:NSMakeRange(20, 3)];
/*阴影偏移*/
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(2, 2);
shadow.shadowColor = [UIColor orangeColor];
shadow.shadowBlurRadius = 1;
[stringaddAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(25, 5)];
/*设置行间距, 段间距, 首行缩进*/
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle
alloc]init];
style.lineSpacing=10;// 行距
style.paragraphSpacing = 20; // 段距
style.firstLineHeadIndent = 30; // 首行缩进
[stringaddAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, string.length)];
/*设置文字的扁平化*/
[stringaddAttribute:NSExpansionAttributeName value:@(1) range:NSMakeRange(35, 8)];
/*在某个位置插入一段字符串*/
NSAttributedString * string1 = [[NSAttributedString alloc]initWithString:@"123"];
[stringinsertAttributedString:string1 atIndex:10];
/*在某个位置替换一段文字*/
[stringreplaceCharactersInRange:NSMakeRange(1, 3) withString:@"hello, oworld"];
/*删除某一段文字*/
[stringdeleteCharactersInRange:NSMakeRange(1, 3)];
/*拼接一段文字*/
[stringappendAttributedString:string];
lable.attributedText= string;
为富文本添加点击方法
/*为富文本添加点击方法*/
self.textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 330, [UIScreen mainScreen].bounds.size.width, 200)];
self.textView.delegate=self;
self.textView.editable=NO;
[self.view addSubview: self.textView];
NSMutableAttributedString * linking = [[NSMutableAttributedString alloc]initWithString:@"我是百度"];
[linkingaddAttribute:NSLinkAttributeName value:@"link://" range:[[linking string] rangeOfString:@"百度"]];
self.textView.attributedText= linking;
在富文本中插入图片
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithAttributedString:self.textView.attributedText];
[attriaddAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, self.textView.text.length)];
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
attch.image= image;
//大小自己调整
attch.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 10,200);
/*生成一个可以添加图片的对象*/
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attriappendAttributedString:string];
self.textView.attributedText= attri;
//需要重新设置下大小
self.textView.font = [UIFont systemFontOfSize:16];
[self.textView setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 2)];