使用NSAttributedString进行文本属性的设置。
设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
NSFontAttributeName
NSMutableAttributedString *textFont = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体大小"];
[textFont addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:18.0]
range:[@"NSAttributedString设置字体大小" rangeOfString:@"NSAttributedString"]];
设置文本段落排版格式,取值为 NSParagraphStyle 对象
NSParagraphStyleAttributeName
NSMutableParagraphStyle *rowParagraph = [[NSMutableParagraphStyle alloc] init];
rowParagraph.firstLineHeadIndent = 20;
rowParagraph.lineSpacing = 10;
NSMutableAttributedString *textRowSpace = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体文字间距"];
[textRowSpace addAttribute:NSParagraphStyleAttributeName value:rowParagraph range:NSMakeRange(0, textRowSpace.string.length)];
设置字体颜色,取值为 UIColor对象,默认值为黑色
NSForegroundColorAttributeName
NSMutableAttributedString *textColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体颜色"];
[textColor addAttribute:NSForegroundColorAttributeName
value:[UIColor greenColor]
range:[@"NSAttributedString设置字体颜色" rangeOfString:@"NSAttributedString"]];
设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
NSBackgroundColorAttributeName
NSMutableAttributedString *textBackColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体背景颜色"];
[textBackColor addAttribute:NSBackgroundColorAttributeName
value:[UIColor yellowColor]
range:[@"NSAttributedString设置字体背景颜色" rangeOfString:@"NSAttributedString"]];
设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
NSLigatureAttributeName设置字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
NSKernAttributeName
NSMutableAttributedString *textSizeSpace = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体文字间距"];
[textSizeSpace addAttribute:NSKernAttributeName
value:@(2)
range:[@"NSAttributedString设置字体文字间距" rangeOfString:@"NSAttributedString"]];
设置删除线,取值为 NSNumber 对象(整数)
NSStrikethroughStyleAttributeName设置删除线颜色,取值为 UIColor 对象,默认值为黑色
NSStrikethroughColorAttributeName
NSMutableAttributedString *textDeleteline = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置删除线"];
[textDeleteline addAttribute:NSStrikethroughStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:[@"NSAttributedString设置删除线" rangeOfString:@"NSAttributedString"]];
[textDeleteline addAttribute:NSStrikethroughColorAttributeName
value:[UIColor blueColor]
range:[@"NSAttributedString设置删除线" rangeOfString:@"NSAttributedString"]];
设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
NSUnderlineStyleAttributeName设置下划线颜色,取值为 UIColor 对象,默认值为黑色
NSUnderlineColorAttributeName
NSMutableAttributedString *textUnderline = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置下划线"];
[textUnderline addAttribute:NSUnderlineStyleAttributeName
value:@(NSUnderlineStyleSingle)
range:[@"NSAttributedString设置下划线" rangeOfString:@"NSAttributedString"]];
[textUnderline addAttribute:NSUnderlineColorAttributeName
value:[UIColor brownColor]
range:[@"NSAttributedString设置下划线" rangeOfString:@"NSAttributedString"]];
设置笔画宽度(粗细),取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
NSStrokeWidthAttributeName
NSMutableAttributedString *textBorderType = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体笔画样式"];
[textBorderType addAttribute:NSStrokeWidthAttributeName
value:@(2)
range:[@"NSAttributedString设置字体笔画样式" rangeOfString:@"NSAttributedString"]];
填充部分颜色,不是字体颜色,取值为 UIColor 对象
NSStrokeColorAttributeName
NSMutableAttributedString *textBorderColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体笔画颜色"];
[textBorderColor addAttribute:NSStrokeColorAttributeName
value:[UIColor greenColor]
range:[@"NSAttributedString设置字体笔画颜色" rangeOfString:@"NSAttributedString"]];
设置阴影属性,取值为 NSShadow 对象
NSShadowAttributeName
// 设置字体阴影 模糊度shadowBlurRadius 阴影颜色shadowColor 阴影偏移shadowOffset
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 1;
shadow.shadowColor = [UIColor purpleColor];
shadow.shadowOffset = CGSizeMake(3, 3);
//
NSMutableAttributedString *textShadow = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体阴影"];
[textShadow addAttribute:NSShadowAttributeName
value:shadow
range:[@"NSAttributedString设置字体阴影" rangeOfString:@"NSAttributedString"]];
设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用
NSTextEffectAttributeName设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
NSBaselineOffsetAttributeName
NSMutableAttributedString *textOffset = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString调整基线位置"];
[textOffset addAttribute:NSBaselineOffsetAttributeName
value:@(6)
range:[@"NSAttributedString调整基线位置" rangeOfString:@"NSAttributedString"]];
设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
NSObliquenessAttributeName
NSMutableAttributedString *textObliqueness = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置文本斜体"];
[textObliqueness addAttribute:NSObliquenessAttributeName
value:@(0.5)
range:[@"NSAttributedString设置文本斜体" rangeOfString:@"NSAttributedString"]];
设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
NSExpansionAttributeName
NSMutableAttributedString *textExpansion = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置文本压缩"];
[textExpansion addAttribute:NSExpansionAttributeName
value:@(0.5)
range:[@"NSAttributedString设置文本压缩" rangeOfString:@"NSAttributedString"]];
设置文字书写方向,从左向右书写或者从右向左书写
NSWritingDirectionAttributeName设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
NSVerticalGlyphFormAttributeName设置链接属性,点击后调用浏览器打开指定URL地址
NSLinkAttributeName
NSMutableAttributedString *textLink = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置超链接"];
[textLink addAttribute:NSLinkAttributeName
value:[NSURL URLWithString:@"http://www.baidu.com"]
range:[@"NSAttributedString设置超链接" rangeOfString:@"NSAttributedString"]];
设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
NSAttachmentAttributeName
// 设置图文混排
// 初始化一个图文对象
NSTextAttachment *attachmentImage = [[NSTextAttachment alloc] init];
attachmentImage.image = [UIImage imageNamed:@"image"];
attachmentImage.bounds = CGRectMake(0, 0, 20.0, 20.0);
NSAttributedString *attributedImage = [NSAttributedString attributedStringWithAttachment:attachmentImage];
//
NSMutableAttributedString *textImage = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString图文混排"];
[textImage insertAttributedString:attributedImage atIndex:[@"NSAttributedString图文混排" rangeOfString:@"NSAttributedString"].length];