iOS AttributedString简介

AttributedString可以分为NSAttributedStringNSMutableAttributedString两种。在使用中通过将 AttributedString赋值给控件的attributedText属性来添加文字样式。

1. NSAttributedString初始化

NSMutableAttributedString继承NSAttributedString,类似于NSMutableStringNSString的关系

// 使用字符串初始化
- (instancetype)initWithString:(NSString *)str;

// 使用字符串初始化,attributes存放一些属性名和属性值
- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSAttributedStringKey, id> *)attrs;

// 使用NSAttributedString初始化
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;

2. NSMutableAttributedString方法

//  为某一范围内的文字添加某个属性的方法
- (void)addAttribute:(NSAttributedStringKey)name value:(id)value range:(NSRange)range;

// 为某一范围内的文字添加多个属性的方法
- (void)addAttributes:(NSDictionary<NSAttributedStringKey, id> *)attrs range:(NSRange)range;

// 移除某个范围内的某个属性的方法
- (void)removeAttribute:(NSAttributedStringKey)name range:(NSRange)range;

3. NSAttributedString文本属性

属性说明
NSFontAttributeName文本的字体(默认是Helvetica(Neue) 12),对应的值是一个UIFont对象
NSForegroundColorAttributeName文本的字体颜色(默认为黑色),对应的值是一个UIColor对象
NSBackgroundColorAttributeName文本的背景颜色(默认是nil),对应的值是一个UIColor对象
NSLigatureAttributeName指某些连在一起的字符,它们采用单个的图元符号(默认值为1)。0表示没有连体字符。1表示使用默认的连体字符
NSKernAttributeName用于调整字距的像素点数(默认是0),对应的值是一个NSNumber对象(整数)
NSStrikethroughStyleAttributeName指定是否在文字上加上删除线(默认是NSUnderlineStyleNone),对应的值是一个NSNumber对象(整数)
NSStrikethroughColorAttributeName指定删除线颜色
NSUnderlineStyleAttributeName指定是否在文字上加上下划线(默认是NSUnderlineStyleNone),对应的值是一个NSNumber对象(整数)
NSUnderlineColorAttributeName指定下划线颜色
NSStrokeWidthAttributeName改变描边宽度(默认为0),所对应的值是一个NSNumber对象(小数)
NSStrokeColorAttributeName指定描边颜色(默认是NSForegroundColorAttributeName),对应的值是一个UIColor对象
NSShadowAttributeName阴影(默认为nil),所对应的值是一个NSShadow对象
NSTextEffectAttributeName设置文本特殊效果,所对应的值是一个NSString对象,目前只有图版印刷效果可用
NSBaselineOffsetAttributeName设置基线偏移值,取值为NSNumber对象(浮点),正值上偏,负值下偏
NSObliquenessAttributeName设置字形倾斜度,取值为NSNumber对象(浮点),正值右倾,负值左倾
NSExpansionAttributeName设置文本横向拉伸属性,取值为NSNumber(浮点),正值横向拉伸文本,负值横向压缩文本
NSWritingDirectionAttributeName设置文字书写方向,从左向右书写或者从右向左书写
NSVerticalGlyphFormAttributeName设置文字排版方向,取值为NSNumber对象(整数),0表示横排文本,1表示竖排文本
NSLinkAttributeName设置链接属性,点击后调用浏览器打开指定URL地址
NSAttachmentAttributeName设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排

NSStrikethroughStyleAttributeNameNSUnderlineStyleAttributeName样式

样式说明
NSUnderlineStyleNone不设置
NSUnderlineStyleSingle细的单线
NSUnderlineStyleThick粗的单线
NSUnderlineStyleDouble细的双线
NSUnderlineStylePatternSolid线样式是连续的实线
NSUnderlineStylePatternDot线样式是虚线点
NSUnderlineStylePatternDash线样式是破折号
NSUnderlineStylePatternDashDot线样式是破折号和点
NSUnderlineStylePatternDashDotDot线样式是破折号和点点
NSUnderlineStyleByWord有空格的地方不设置

示例代码

NSMutableAttributedString *fontText = [[NSMutableAttributedString alloc] initWithString:@"正常字体25号粗体"];
[fontText setAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:25]} range:NSMakeRange(4, 5)];

NSMutableAttributedString *colorText = [[NSMutableAttributedString alloc] initWithString:@"红色字体蓝色背景"];
[colorText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 4)];
[colorText addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(4, 4)];
    
NSMutableAttributedString *kernText = [[NSMutableAttributedString alloc] initWithString:@"间距为5间距为10"];
[kernText setAttributes:@{NSKernAttributeName:@5, NSBackgroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, 4)];
[kernText setAttributes:@{NSKernAttributeName:@10, NSBackgroundColorAttributeName:[UIColor blueColor]} range:NSMakeRange(4, 5)];

NSMutableAttributedString *lineLabel = [[NSMutableAttributedString alloc] initWithString:@"删除线下划线"];
[lineLabel addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle|NSUnderlinePatternDot) range:NSMakeRange(0, 3)];
[lineLabel addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];

[lineLabel addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(3, 3)];
[lineLabel addAttribute:NSUnderlineColorAttributeName value:[UIColor blueColor] range:NSMakeRange(3, 3)];

NSMutableAttributedString *strokeText = [[NSMutableAttributedString alloc] initWithString:@"描边2描边4"];
[strokeText addAttribute:NSStrokeWidthAttributeName value:@(2) range:NSMakeRange(0, 3)];
[strokeText addAttribute:NSStrokeWidthAttributeName value:@(4) range:NSMakeRange(3, 3)];
[strokeText addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(1, 4)];

NSMutableAttributedString *shadowText = [[NSMutableAttributedString alloc] initWithString:@"字体阴影"];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor magentaColor];
shadow.shadowOffset = CGSizeMake(10, 5);
[shadowText addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, 4)];

NSMutableAttributedString *baselineOffsetText = [[NSMutableAttributedString alloc] initWithString:@"正值上偏负值下偏"];
[baselineOffsetText addAttribute:NSBaselineOffsetAttributeName value:@(5) range:NSMakeRange(2, 2)];
[baselineOffsetText addAttribute:NSBaselineOffsetAttributeName value:@(-5) range:NSMakeRange(6, 2)];

NSMutableAttributedString *obliquenessText = [[NSMutableAttributedString alloc] initWithString:@"负值左倾正值右倾"];
[obliquenessText addAttribute:NSObliquenessAttributeName value:@(-0.5) range:NSMakeRange(2, 2)];
[obliquenessText addAttribute:NSObliquenessAttributeName value:@(0.5) range:NSMakeRange(6, 2)];

NSMutableAttributedString *expansionText = [[NSMutableAttributedString alloc] initWithString:@"负值压缩正值拉伸"];
[expansionText addAttribute:NSExpansionAttributeName value:@(-0.5) range:NSMakeRange(0, 4)];
[expansionText addAttribute:NSExpansionAttributeName value:@(0.5) range:NSMakeRange(4, 4)];

NSMutableAttributedString *writingDirectionText = [[NSMutableAttributedString alloc] initWithString:@"文字排版从右往左"];
[writingDirectionText addAttribute:NSWritingDirectionAttributeName value:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)] range:NSMakeRange(0, 8)];

NSMutableAttributedString *attachmentText = [[NSMutableAttributedString alloc] initWithString:@"图文混排"];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"icon_star"];
attachment.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString *attrImage = [NSAttributedString attributedStringWithAttachment: attachment];
[attachmentText appendAttributedString:attrImage];

显示如下
在这里插入图片描述

4. NSAttributedString段落属性

NSParagraphStyleAttributeName,段落属性(默认是defaultParagraphStyle),对应的值是NSParagraphStyle对象。

NSMutableParagraphStyle属性

// 行间距
@property CGFloat lineSpacing;
// 段与段之间的间距
@property CGFloat paragraphSpacing;
// 文本两端对齐方式
@property NSTextAlignment alignment;
// 首行缩进
@property CGFloat firstLineHeadIndent;
// 整体缩进(首行除外)
@property CGFloat headIndent;
// 结尾部分的内容超出部分如何显示
@property CGFloat tailIndent;
// 断行方式
@property NSLineBreakMode lineBreakMode;
// 最低行高
@property CGFloat minimumLineHeight;
// 最大行高
@property CGFloat maximumLineHeight;
// 书写方向
@property NSWritingDirection baseWritingDirection;
// 可变行高
@property CGFloat lineHeightMultiple;
// 段首行空白空间
@property CGFloat paragraphSpacingBefore;
// 连字属性,iOS支持的值分别为0和1
@property float hyphenationFactor;

示例代码

// 行间距5
NSMutableAttributedString *lineSpacingText = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *lineSpacingParagraphStyle = [[NSMutableParagraphStyle alloc] init];
lineSpacingParagraphStyle.lineSpacing = 5;
[lineSpacingText addAttribute:NSParagraphStyleAttributeName value:lineSpacingParagraphStyle range:NSMakeRange(0, [text length])];

// 首行缩进20,其余行缩进10
NSMutableAttributedString *lineHeadIndentText = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *lineHeadIndentParagraphStyle = [[NSMutableParagraphStyle alloc] init];
lineHeadIndentParagraphStyle.firstLineHeadIndent = 20;
lineHeadIndentParagraphStyle.headIndent = 10;
[lineHeadIndentText addAttribute:NSParagraphStyleAttributeName value:lineHeadIndentParagraphStyle range:NSMakeRange(0, [text length])];

// 段落居中显示
NSMutableAttributedString *alignmentText = [[NSMutableAttributedString alloc] initWithString:text];
NSMutableParagraphStyle *alignmentParagraphStyle = [[NSMutableParagraphStyle alloc] init];
alignmentParagraphStyle.alignment = NSTextAlignmentCenter;
[alignmentText addAttribute:NSParagraphStyleAttributeName value:alignmentParagraphStyle range:NSMakeRange(0, [text length])];

显示如下
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值