NSAttributedString文本属性设置的使用

// 设置字体属性NSFontAttributeName,默认值:字体:Helvetica(Neue) 字号:12 
NSMutableAttributedString *textFont = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体大小"];
[textFont addAttribute:NSFontAttributeName
                 value:[UIFont boldSystemFontOfSize:18.0]
                 range:[@"NSAttributedString设置字体大小" rangeOfString:@"NSAttributedString"]];

效果图-字体大小

// 设置文本段落排版格式NSParagraphStyleAttributeName,取值为 NSParagraphStyle 对象 
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)];

效果图-文字排版

// 设置字体颜色NSForegroundColorAttributeName,取值为 UIColor对象,默认值为黑色 
NSMutableAttributedString *textColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体颜色"];
[textColor addAttribute:NSForegroundColorAttributeName
                  value:[UIColor greenColor]
                  range:[@"NSAttributedString设置字体颜色" rangeOfString:@"NSAttributedString"]];

效果图-字体颜色

// 设置字体所在区域背景颜色NSBackgroundColorAttributeName,取值为 UIColor对象,默认值为nil, 透明色 
NSMutableAttributedString *textBackColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体背景颜色"];
[textBackColor addAttribute:NSBackgroundColorAttributeName
                      value:[UIColor yellowColor]
                      range:[@"NSAttributedString设置字体背景颜色" rangeOfString:@"NSAttributedString"]];

效果图-字体背景颜色

/*
设置连体属性NSLigatureAttributeName,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符 
设置字符间距NSKernAttributeName,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄 
*/
NSMutableAttributedString *textSizeSpace = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体文字间距"];
[textSizeSpace addAttribute:NSKernAttributeName
                      value:@(2)
                      range:[@"NSAttributedString设置字体文字间距" rangeOfString:@"NSAttributedString"]];

效果图-文字间距

/*
设置删除线NSStrikethroughStyleAttributeName,取值为 NSNumber 对象(整数)  
设置删除线颜色NSStrikethroughColorAttributeName,取值为 UIColor 对象,默认值为黑色 
*/
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"]];

效果图-删除线

/*
设置下划线NSUnderlineStyleAttributeName,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似  
设置下划线颜色NSUnderlineColorAttributeName,取值为 UIColor 对象,默认值为黑色 
*/
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"]];

效果图-下划线

// 设置笔画宽度(粗细) NSStrokeWidthAttributeName,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果 
NSMutableAttributedString *textBorderType = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体笔画样式"];
[textBorderType addAttribute:NSStrokeWidthAttributeName
                       value:@(2)
                       range:[@"NSAttributedString设置字体笔画样式" rangeOfString:@"NSAttributedString"]];

效果图-字体笔画线宽

// 填充部分颜色NSStrokeColorAttributeName,不是字体颜色,取值为 UIColor 对象(无效果) 
NSMutableAttributedString *textBorderColor = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体笔画颜色"];
[textBorderColor addAttribute:NSStrokeColorAttributeName
                        value:[UIColor greenColor]
                        range:[@"NSAttributedString设置字体笔画颜色" rangeOfString:@"NSAttributedString"]];

效果图-字体笔画颜色

// 设置阴影属性NSShadowAttributeName,取值为 NSShadow 对象 
// 设置字体阴影 模糊度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"]];

效果图-阴影

/*
设置文本特殊效果NSTextEffectAttributeName,取值为 NSString 对象,目前只有图版印刷效果可用 
设置基线偏移值NSBaselineOffsetAttributeName,取值为 NSNumber (float),正值上偏,负值下偏 
*/
NSMutableAttributedString *textOffset = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString调整基线位置"];
[textOffset addAttribute:NSBaselineOffsetAttributeName
                   value:@(6)
                   range:[@"NSAttributedString调整基线位置" rangeOfString:@"NSAttributedString"]];

效果图-基线调整

// 设置字形倾斜度NSObliquenessAttributeName,取值为 NSNumber (float),正值右倾,负值左倾 
NSMutableAttributedString *textObliqueness = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置文本斜体"];
[textObliqueness addAttribute:NSObliquenessAttributeName
                        value:@(0.5)
                        range:[@"NSAttributedString设置文本斜体" rangeOfString:@"NSAttributedString"]];

效果图-字体倾斜

// 设置文本横向拉伸属性NSExpansionAttributeName,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本 
NSMutableAttributedString *textExpansion = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置文本压缩"];
[textExpansion addAttribute:NSExpansionAttributeName
                      value:@(0.5)
                      range:[@"NSAttributedString设置文本压缩" rangeOfString:@"NSAttributedString"]];

效果图-文本压缩

// 设置文字书写方向NSWritingDirectionAttributeName,从左向右书写或者从右向左书写
NSString *string = @"NSWritingDirectionAttributeName书字方向";
NSMutableAttributedString *attibutedStr = [[NSMutableAttributedString alloc] initWithString:string];
[attibutedStr addAttribute:NSWritingDirectionAttributeName
                     value:@[@(3)]
                     range:NSMakeRange(0, string.length)];

效果图-书写方向

// 设置文字排版方向NSVerticalGlyphFormAttributeName,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本 (无效果)
NSString *string = @"NSVerticalGlyphFormAttributeName排版方向";
NSMutableAttributedString *attibutedStr = [[NSMutableAttributedString alloc] initWithString:string];
[attibutedStr addAttribute:NSVerticalGlyphFormAttributeName
                     value:@(3)
                    range:NSMakeRange(0, string.length)];

效果图-排版方向

/*
设置链接属性NSLinkAttributeName,点击后调用浏览器打开指定URL地址 
*/
NSMutableAttributedString *textLink = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置超链接"];
[textLink addAttribute:NSLinkAttributeName
                 value:[NSURL URLWithString:@"http://www.baidu.com"]
                 range:[@"NSAttributedString设置超链接" rangeOfString:@"NSAttributedString"]];

效果图-超链接

// 设置文本附件NSAttachmentAttributeName,取值为NSTextAttachment对象,常用于文字图片混排 
// 设置图文混排
// 初始化一个图文对象
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];

猜你喜欢

转载自blog.csdn.net/Lea__DongYang/article/details/83148838