属性总结
NSFontAttributeName 设置字体大小和字体的类型 默认12 Helvetica(Neue)
NSForegroundColorAttributeName 设置字体颜色,默认黑色 UIColor对象
NSBackgroundColorAttributeName 设置字体所在区域的背景颜色,默认为nil,透明色
NSLigatureAttributeName 设置连体属性,NSNumber对象 默认0 没有连体
NSKernAttributeName 设置字符间距, NSNumber浮点型属性 正数间距加大,负数间距缩小
NSStrikethroughStyleAttributeName 设置删除线,NSNumber对象
NSStrikethroughColorAttributeName 设置删除线颜色,UIColor对象,默认是黑色
NSUnderlineStyleAttributeName 设置下划线,NSNumber对象 NSUnderlineStyle枚举值
NSUnderlineColorAttributeName 设置下划线颜色,UIColor对象,默认是黑色
NSStrokeWidthAttributeName 设置笔画宽度,NSNumber对象 正数中空 负数填充
NSStrokeColorAttributeName 设置填充部分颜色,不是指字体颜色,UIColor对象
NSShadowAttributeName 设置阴影属性,取值为NSShadow对象
NSTextEffectAttributeName 设置文本特殊效果 NSString对象 只有图版印刷效果可用
NSBaselineOffsetAttributeName 设置基线偏移量,NSNumber float对象 正数向上偏移,负数向下偏移
NSObliquenessAttributeName 设置字体倾斜度,NSNumber float对象,正数右倾斜,负数左倾斜
NSExpansionAttributeName 设置文本横向拉伸属性,NSNumber float对象,正数横向拉伸文本,负数压缩
NSWritingDirectionAttributeName 设置文字书写方向,从左向右或者右向左
NSVerticalGlyphFormAttributeName 设置文本排版方向,NSNumber对象。0 横向排版,1 竖向排版
NSLinkAttributeName 设置文本超链接,点击可以打开指定URL地址
NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,一般为图文混排
NSParagraphStyleAttributeName 设置文本段落排版,为NSParagraphStyle对象
新增NSParagraphStyleAttributeName的属性介绍
paragraphStyle.lineSpacing=0.0;增加行高paragraphStyle.headIndent=0;//头部缩进,相当于左paddingparagraphStyle.tailIndent=0;//相当于右paddingparagraphStyle.lineHeightMultiple=0;//行间距是多少倍paragraphStyle.alignment=NSTextAlignmentLeft;//对齐方式paragraphStyle.firstLineHeadIndent=0;//首行头缩进paragraphStyle.paragraphSpacing=0;//段落后面的间距paragraphStyle.paragraphSpacingBefore=0;//段落之前的间距
1.NSFontAttributeName
说明:该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)
2.NSForegroundColorAttributeName
说明:该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色
3.NSBackgroundColorAttributeName
说明:设置文字背景颜色
NSString *string = @"落霞与孤鹜齐飞,秋水共长天一色";
NSMutableAttributedString *mutableAttriteStr = [[NSMutableAttributedString alloc] init];
NSAttributedString *attributeStr1 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(0, 3)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"futura" size:12],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor yellowColor]}];
NSAttributedString *attributeStr4 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(3, 4)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"futura" size:22],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor yellowColor]}];
NSAttributedString *attributeStr2 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(8, 4)] attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:22],NSForegroundColorAttributeName : [UIColor blackColor],NSBackgroundColorAttributeName : [UIColor lightGrayColor]}];
NSAttributedString *attributeStr5 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(12, 3)] attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName : [UIColor blackColor],NSBackgroundColorAttributeName : [UIColor lightGrayColor]}];
NSAttributedString *attriteStr3 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(7, 1)] attributes:@{NSBackgroundColorAttributeName : [UIColor greenColor]}];
[mutableAttriteStr appendAttributedString:attributeStr1];
[mutableAttriteStr appendAttributedString:attributeStr4];
[mutableAttriteStr appendAttributedString:attriteStr3];
[mutableAttriteStr appendAttributedString:attributeStr2];
4.NSLigatureAttributeName
连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1
表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS不支持值
为 2)
self.label2.attributedText = [[NSAttributedString alloc] initWithString:@"flush and fily" attributes:@{NSLigatureAttributeName : [NSNumber numberWithInt:1],NSFontAttributeName : [UIFont fontWithName:@"futura" size:30]}];
NSMutableAttributedString *mutableAttributeStr2 = [[NSMutableAttributedString alloc] init];
NSAttributedString *string6 = [[NSAttributedString alloc] initWithString:@"ABCDE " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:-3],NSForegroundColorAttributeName : [UIColor redColor]}];
NSAttributedString *string7 = [[NSAttributedString alloc] initWithString:@"FGHIJ " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:0],NSForegroundColorAttributeName : [UIColor yellowColor]}];
NSAttributedString *string8 = [[NSAttributedString alloc] initWithString:@"KLMNO " attributes:@{NSKernAttributeName : @(15),NSForegroundColorAttributeName : [UIColor blueColor]}];
[mutableAttributeStr2 appendAttributedString:string6];
[mutableAttributeStr2 appendAttributedString:string7];
[mutableAttributeStr2 appendAttributedString:string8];
self.label3.attributedText = mutableAttributeStr2;