iOS 关于AttributedString富文本简单使用以及知识点

今天在看label的时候,看到这个属性,有搜了一些资料,这个总结的挺好的,就直接拿来了,大家可以看看,对文本编辑会有很大的用。

使用方式一

初始化一个NSMutableAttributedString,然后向里面添加文字样式,将其赋给控件的attributedText属性。(这种方式只是简单的用几个属性表示了用法,在下面的一种使用方式中,会详细总结所有属性)

AttributedString可以分为NSAttributedString和NSMutableAttributedString两种。在使用中通过将AttributedString赋值给控件的 attributedText 属性来添加文字样式。有属性的控件有UILabel、UITextField和UITextView。
NSString *str = @"人生若只如初见,何事悲风秋画扇。\n等闲变却故人心,却道故人心易变。\n骊山语罢清宵半,泪雨霖铃终不怨。\n何如薄幸锦衣郎,比翼连枝当日愿。";
//创建NSMutableAttributedString
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str];

//设置字体和设置字体的范围
[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30.0f] range:NSMakeRange(0, 3)];
//添加文字颜色
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(17, 7)];
//添加文字背景颜色
[attrStr addAttribute:NSBackgroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(17, 7)];
//添加下划线
[attrStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(8, 7)];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 100, 200, 0)];
label.backgroundColor = [UIColor greenColor];
//自动换行
label.numberOfLines = 0;
//设置label的富文本
label.attributedText = attrStr;
//label高度自适应
[label sizeToFit];
[self.view addSubview:label];

使用方式二

创建属性字典,并将各种属性初始化。赋值,并利用方法appendAttributedString:添加入NSMutableAttributedString,将其赋给控件的attributedText属性。

注释感觉写的挺详细,就直接贴代码了:

//初始化NSMutableAttributedString
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]init];

//设置字体格式和大小
NSString *str0 = @"设置字体格式和大小";
NSDictionary *dictAttr0 = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
NSAttributedString *attr0 = [[NSAttributedString alloc]initWithString:str0 attributes:dictAttr0];
[attributedString appendAttributedString:attr0];

//设置字体颜色
NSString *str1 = @"\n设置字体颜色\n";
NSDictionary *dictAttr1 = @{NSForegroundColorAttributeName:[UIColor purpleColor]};
NSAttributedString *attr1 = [[NSAttributedString alloc]initWithString:str1 attributes:dictAttr1];
[attributedString appendAttributedString:attr1];

//设置字体背景颜色
NSString *str2 = @"设置字体背景颜色\n";
NSDictionary *dictAttr2 = @{NSBackgroundColorAttributeName:[UIColor cyanColor]};
NSAttributedString *attr2 = [[NSAttributedString alloc]initWithString:str2 attributes:dictAttr2];
[attributedString appendAttributedString:attr2];


//设置连体属性
NSString *str3 = @"fly";
NSDictionary *dictAttr3 = @{NSFontAttributeName:[UIFont fontWithName:@"futura" size:14],NSLigatureAttributeName:[NSNumber numberWithInteger:1]};
NSAttributedString *attr3 = [[NSAttributedString alloc]initWithString:str3 attributes:dictAttr3];
[attributedString appendAttributedString:attr3];



NSString *str4 = @"\n设置字符间距";
NSDictionary *dictAttr4 = @{NSKernAttributeName:@(4)};
NSAttributedString *attr4 = [[NSAttributedString alloc]initWithString:str4 attributes:dictAttr4];
[attributedString appendAttributedString:attr4];



NSString *str51 = @"\n设置删除线为细单实线,颜色为红色";
NSDictionary *dictAttr51 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),NSStrikethroughColorAttributeName:[UIColor redColor]};
NSAttributedString *attr51 = [[NSAttributedString alloc]initWithString:str51 attributes:dictAttr51];
[attributedString appendAttributedString:attr51];


NSString *str52 = @"\n设置删除线为粗单实线,颜色为红色";
NSDictionary *dictAttr52 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick),NSStrikethroughColorAttributeName:[UIColor redColor]};
NSAttributedString *attr52 = [[NSAttributedString alloc]initWithString:str52 attributes:dictAttr52];
[attributedString appendAttributedString:attr52];

NSString *str53 = @"\n设置删除线为细单实线,颜色为红色";
NSDictionary *dictAttr53 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleDouble),NSStrikethroughColorAttributeName:[UIColor redColor]};
NSAttributedString *attr53 = [[NSAttributedString alloc]initWithString:str53 attributes:dictAttr53];
[attributedString appendAttributedString:attr53];


NSString *str54 = @"\n设置删除线为细单虚线,颜色为红色";
NSDictionary *dictAttr54 = @{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternDot),NSStrikethroughColorAttributeName:[UIColor redColor]};
NSAttributedString *attr54 = [[NSAttributedString alloc]initWithString:str54 attributes:dictAttr54];
[attributedString appendAttributedString:attr54];


//设置笔画宽度和填充部分颜色
NSString *str6 = @"设置笔画宽度和填充颜色\n";
NSDictionary *dictAttr6 = @{NSStrokeWidthAttributeName:@(2),NSStrokeColorAttributeName:[UIColor blueColor]};
NSAttributedString *attr6 = [[NSAttributedString alloc]initWithString:str6 attributes:dictAttr6];
[attributedString appendAttributedString:attr6];

//设置阴影属性,取值为NSShadow对象
NSString *str7 = @"设置阴影属性\n";
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowColor = [UIColor redColor];
shadow.shadowBlurRadius = 1.0f;
shadow.shadowOffset = CGSizeMake(1, 1);
NSDictionary *dictAttr7 = @{NSShadowAttributeName:shadow};
NSAttributedString *attr7 = [[NSAttributedString alloc]initWithString:str7 attributes:dictAttr7];
[attributedString appendAttributedString:attr7];

//设置文本特殊效果,取值为NSString类型,目前只有一个可用效果  NSTextEffectLetterpressStyle(凸版印刷效果)
NSString *str8 = @"设置特殊效果\n";
NSDictionary *dictAttr8 = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
NSAttributedString *attr8 = [[NSAttributedString alloc]initWithString:str8 attributes:dictAttr8];
[attributedString appendAttributedString:attr8];

//设置文本附件,取值为NSTextAttachment对象,常用于文字的图文混排
NSString *str9 = @"文字的图文混排\n";
NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];
textAttachment.image = [UIImage imageNamed:@"logo.png"];
textAttachment.bounds = CGRectMake(0, 0, 30, 30);
NSDictionary *dictAttr9 = @{NSAttachmentAttributeName:textAttachment};
NSAttributedString *attr9 = [[NSAttributedString alloc]initWithString:str9 attributes:dictAttr9];
[attributedString appendAttributedString:attr9];


//添加下划线
NSString *str10 = @"添加下划线\n";
NSDictionary *dictAttr10 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),NSUnderlineColorAttributeName:[UIColor redColor]};
NSAttributedString *attr10 = [[NSAttributedString alloc]initWithString:str10 attributes:dictAttr10];
[attributedString appendAttributedString:attr10];


//设置基线偏移值 NSBaselineOffsetAttributeName
NSString *str11 = @"添加基线偏移值\n";
NSDictionary *dictAttr11 = @{NSBaselineOffsetAttributeName:@(-10)};
NSAttributedString *attr11 = [[NSAttributedString alloc]initWithString:str11 attributes:dictAttr11];
[attributedString appendAttributedString:attr11];


//设置字体倾斜度 NSObliquenessAttributeName
NSString *str12 = @"设置字体倾斜度\n";
NSDictionary *dictAttr12 = @{NSObliquenessAttributeName:@(0.5)};
NSAttributedString *attr12 = [[NSAttributedString alloc]initWithString:str12 attributes:dictAttr12];
[attributedString appendAttributedString:attr12];


//设置字体的横向拉伸 NSExpansionAttributeName
NSString *str13 = @"设置字体横向拉伸\n";
NSDictionary *dictAttr13 = @{NSExpansionAttributeName:@(0.5)};
NSAttributedString *attr13 = [[NSAttributedString alloc]initWithString:str13 attributes:dictAttr13];
[attributedString appendAttributedString:attr13];


//设置文字的书写方向 NSWritingDirectionAttributeName
NSString *str14 = @"设置文字书写方向\n";
NSDictionary *dictAttr14 = @{NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]};
NSAttributedString *attr14 = [[NSAttributedString alloc]initWithString:str14 attributes:dictAttr14];
[attributedString appendAttributedString:attr14];


//设置文字排版方向 NSVerticalGlyphFormAttributeName
NSString *str15 = @"设置文字排版方向\n";
NSDictionary *dictAttr15 = @{NSVerticalGlyphFormAttributeName:@(0)};
NSAttributedString *attr15 = [[NSAttributedString alloc]initWithString:str15 attributes:dictAttr15];
[attributedString appendAttributedString:attr15];

//段落样式
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
//行间距
paragraph.lineSpacing = 10;
//段落间距
paragraph.paragraphSpacing = 20;
//对齐方式
paragraph.alignment = NSTextAlignmentLeft;
//指定段落开始的缩进像素
paragraph.firstLineHeadIndent = 30;
//调整全部文字的缩进像素
paragraph.headIndent = 10;

//添加段落设置
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, attributedString.length)];

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 100, 300, 0)];
label.backgroundColor = [UIColor lightGrayColor];
//自动换行
label.numberOfLines = 0;
//设置label的富文本
label.attributedText = attributedString;
//label高度自适应
[label sizeToFit];
[self.view addSubview:label];

记录下AttributedString的知识点

//通过已知富文本内容以及宽度获取高度
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 6_0);
//如:
CGRect frame;
float textMaxWidth = frame.size.width;
NSMutableAttributedString *attrStr = [Utility emotionStrWithString:message.content plistName:@"test.plist" y:0];
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:16.0]
                    range:NSMakeRange(0, attrStr.length)];
CGSize textSize = [attrStr boundingRectWithSize:CGSizeMake(textMaxWidth, CGFLOAT_MAX)
                                            options:NSStringDrawingUsesLineFragmentOrigin
                                            context:nil].size;

NSStringDrawingOptions四种类型解释

如果options参数为NSStringDrawingUsesLineFragmentOrigin,那么整个文本将以每行组成的矩形为单位计算整个文本的尺寸。
(在这里有点奇怪,因为字体高度大概是13.8,textView中大概有10行文字,此时用该选项计算出来的只有5行,即高度为69。
而同时使用
NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin却可以得出文字刚好有10行,即高度为138,这里要等iOS7官方的文档出来再看看选项的说明,因为毕竟以上文档是iOS6的东西)
如果为
NSStringDrawingTruncatesLastVisibleLine
或者NSStringDrawingUsesDeviceMetric,那么计算文本尺寸时将以每个字或字形为单位来计算。
如果为
NSStringDrawingUsesFontLeading则以字体间的行距(leading,行距:从一行文字的底部到另一行文字底部的间距。)来计算。
各个参数是可以组合使用的,如NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine。
根据该方法调整一下Reader的分页方法:(主要是将被iOS7 Deprecated的sizeWithFont:constrainedToSize:lineBreakMode:方法改成了boudingRectWithSize:options:attributes:context:方法来计算文本尺寸)

具体使用可参考:AttributedStringde的使用
参考文章:iOS富文本AttributedString的总结

你可能感兴趣的:(iOS 关于AttributedString富文本简单使用以及知识点)