###一、Attributes字典键值对照表
键 | 值类型 | 备注 |
---|---|---|
NSKernAttributeName | NSNumber | 文字间距,支持浮点型 |
NSFontAttributeName | UIFont | 字体 |
NSForegroundColorAttributeName | UIColor | 颜色 |
NSParagraphStyleAttributeName | NSMutableParagraphStyle | 段落样式,详细见【1】 |
NSBackgroundColorAttributeName | UIColor | 字体的所占区域的背景颜色 |
NSStrokeColorAttributeName | UIColor | 字体边框的颜色 |
NSStrokeWidthAttribute | NSNumber | 边框的宽度 |
NSStrikethroughStyleAttributeName | NSNumber | 删除线 |
NSUnderlineStyleAttributeName | NSNumber | 下划线 |
NSVerticalGlyphFormAttributeName | NSNumber | 文字排版方向0 表示横排文本.1 表示竖排文本,iOS只有横排 |
NSObliquenessAttributeName | NSNumber | 文字倾斜程度 |
NSExpansionAttributeName | NSNumber | 文字的扁平化程度 |
NSShadowAttributeName | NSShadow | 文字阴影,见【2】 |
【1】NSMutableParagraphStyle类使用范例
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];paragraphStyle.lineSpacing = 10;// 字体的行间距paragraphStyle.firstLineHeadIndent = 20.0f;//首行缩进paragraphStyle.alignment = NSTextAlignmentJustified;//(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;//结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")paragraphStyle.headIndent = 20;//整体缩进(首行除外)paragraphStyle.tailIndent = 20;//尾部缩进paragraphStyle.minimumLineHeight = 10;//最低行高paragraphStyle.maximumLineHeight = 20;//最大行高paragraphStyle.paragraphSpacing = 15;//段与段之间的间距paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//从左到右的书写方向(一共➡️⬇️⬅️三种)paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */paragraphStyle.hyphenationFactor = 1;//连字属性 在iOS,唯一支持的值分别为0和1复制代码
【2】NSShadowAttributeName对应的是NSShadow对象,单一使用不会有任何效果,需要配合着NSVerticalGlyphFormAttributeName(文字排版方向)、NSObliquenessAttributeName(文字倾斜)、NSExpansionAttributeName(文字扁平化)配合使用,NSShadow相关属性设置如下所示.
NSShadow *shadow = [[NSShadow alloc]init];shadow.shadowBlurRadius = 5;//模糊度shadow.shadowColor = [UIColor whiteColor];//阴影颜色shadow.shadowOffset = CGSizeMake(1, 5);//阴影的大小复制代码
二、 NSAttributedString与NSMutableAttributedString
1.NSAttributedString 属性
//均为只读@property (readonly, copy) NSString *string;@property (readonly) NSUInteger length;复制代码
创建方法
- (instancetype)initWithString:(NSString *)str;- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary*)attrs;复制代码
其他方法
//富文本字符串比较- (BOOL)isEqualToAttributedString:(NSAttributedString *)other;复制代码
2.NSMutableAttributedString 作为NSAttributedString的子类增加了以下动态添加富文本样式的方法,见文思意,也不必过多解释。
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;- (void)addAttributes:(NSDictionary*)attrs range:(NSRange)range;- (void)removeAttribute:(NSString *)name range:(NSRange)range;- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;- (void)appendAttributedString:(NSAttributedString *)attrString;//完全替换掉attributeString的内容- (void)setAttributedString:(NSAttributedString *)attrString;复制代码
我是分割区~~~大神,女朋友喊你回家吃饭,你可以先走了。
###三、demo 如果以上看完了,还不懂怎么使用的朋友。那不妨看看在下拙劣的demo吧。(注:Label的背景颜色都是在xib(nib)中设置的)
- 文字间距
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"改变文字间距,间距为6"attributes:@{NSKernAttributeName:@6}]; _label1.attributedText = string;复制代码
- 字体
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"改变文字字体,20号粗体"attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20]}]; _label2.attributedText = string;复制代码
- 颜色
NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"改变文字颜色,绿色"attributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}]; _label3.attributedText = string;复制代码
- 描边和空心
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"改变文字边框颜色宽度,这两者协同使用才有效果,宽度正数为空心(前景色失效),负数描边"]; //空心 [mutableAttributedstring addAttribute:NSStrokeWidthAttributeName value:@3.2 range:NSMakeRange(0, 37)]; [mutableAttributedstring addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 37)]; //描边 [mutableAttributedstring addAttribute:NSStrokeWidthAttributeName value:@-5 range:NSMakeRange(38, mutableAttributedstring.length - 38)]; [mutableAttributedstring addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(38, mutableAttributedstring.length - 38)]; _label4.numberOfLines = 3; _label4.attributedText = mutableAttributedstring;复制代码
- 删除线
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"一条删除线,两条删除线,加粗删除线"]; [mutableAttributedstring addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,5)]; [mutableAttributedstring addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:NSMakeRange(6,5)]; [mutableAttributedstring addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:NSMakeRange(12,5)]; _label5.attributedText = mutableAttributedstring;复制代码
- 下划线
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"一条下划线,两条下划线,加粗下划线"]; [mutableAttributedstring addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,5)]; [mutableAttributedstring addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleDouble] range:NSMakeRange(6,5)]; [mutableAttributedstring addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:NSMakeRange(12,5)]; _label6.attributedText = mutableAttributedstring;复制代码
- 斜体、阴影和压扁
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"文字倾斜,文字阴影,压扁文字"]; [mutableAttributedstring addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(0,4)]; //文字阴影 NSShadow *shadow = [[NSShadow alloc] init]; //模糊度 shadow.shadowBlurRadius = 5; //阴影颜色 shadow.shadowColor = [UIColor yellowColor]; //阴影大小 shadow.shadowOffset = CGSizeMake(2, 5); [mutableAttributedstring addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(5,4)]; //压扁文字 [mutableAttributedstring addAttribute:NSExpansionAttributeName value:@0.8 range:NSMakeRange(10,4)]; _label7.numberOfLines = 2; _label7.attributedText = mutableAttributedstring;复制代码
- 段落样式
NSMutableAttributedString *mutableAttributedstring = [[NSMutableAttributedString alloc] initWithString:@"据外媒报道,美国太空探索技术公司SpaceX原定于北京时间2月18日23时01分,在佛罗里达州发射“龙”无人货运飞船,运送补给物资前往国际太空站,并尝试回收搭载飞船的“猎鹰9”号火箭。但二级火箭在倒计时13秒时因引擎故障,宣布取消发射。\n在发射暂停后约10分钟后,SpaceX首席执行官埃隆·马斯克(Elon Musk)在社交媒体上发布消息称:“一切运行正常,除了二级火箭发动机转向液压活塞的运动轨迹稍显奇怪,需要进行调查。”"]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; // 字体的行间距 paragraphStyle.lineSpacing = 10; //首行缩进 paragraphStyle.firstLineHeadIndent = 20.0f; //(两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)// paragraphStyle.alignment = NSTextAlignmentLeft; //结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")// paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; paragraphStyle.headIndent = 20;//整体缩进(首行除外) //设置尾部缩进后就出问题// paragraphStyle.tailIndent = 2;//尾部缩进 paragraphStyle.minimumLineHeight = 10;//最低行高 paragraphStyle.maximumLineHeight = 20;//最大行高// paragraphStyle.paragraphSpacing = 15;//段与段之间的间距 paragraphStyle.paragraphSpacingBefore = 22.0f;//段首行空白空间/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */ paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;//从左到右的书写方向(一共➡️⬇️⬅️三种) paragraphStyle.lineHeightMultiple = 15;/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */ paragraphStyle.hyphenationFactor = 1;//连字属性 在iOS,唯一支持的值分别为0和1 [mutableAttributedstring addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mutableAttributedstring.length)]; [mutableAttributedstring addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, mutableAttributedstring.length)]; _label8.numberOfLines = MAXFLOAT; _label8.attributedText = mutableAttributedstring;复制代码
常用的也就这些了吧,更深入还是用富文本框架吧。