NSAttributedString文本属性设置的使用

使用NSAttributedString可以进行个性化的文本属性显示设置。

NSAttributedString在使用时,共有多个属性可以行设置使用。

// 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
NSFontAttributeName

NSMutableAttributedString *textFont = [[NSMutableAttributedString alloc] initWithString:@"NSAttributedString设置字体大小"];
[textFont addAttribute:NSFontAttributeName
                 value:[UIFont boldSystemFontOfSize:18.0]
                 range:[@"NSAttributedString设置字体大小" rangeOfString:@"NSAttributedString"]];

 

// 设置文本段落排版格式,取值为 NSParagraphStyle 对象(详情见下面的API说明)
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

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

NSString *string = @"NSWritingDirectionAttributeName书字方向";
NSMutableAttributedString *attibutedStr = [[NSMutableAttributedString alloc] initWithString:string];
[attibutedStr addAttribute:NSWritingDirectionAttributeName
                     value:@[@(3)]
                     range:NSMakeRange(0, string.length)];

 

// 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
NSVerticalGlyphFormAttributeName

NSString *string = @"NSVerticalGlyphFormAttributeName排版方向";
NSMutableAttributedString *attibutedStr = [[NSMutableAttributedString alloc] initWithString:string];
[attibutedStr addAttribute:NSVerticalGlyphFormAttributeName
                     value:@(3)
                    range:NSMakeRange(0, string.length)];

 

// 设置链接属性,点击后调用浏览器打开指定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];

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值