AttributedString
可以分为NSAttributedString
和NSMutableAttributedString
两种。在使用中通过将 AttributedString
赋值给控件的attributedText
属性来添加文字样式。
1. NSAttributedString初始化
NSMutableAttributedString
继承NSAttributedString
,类似于NSMutableString
和NSString
的关系
// 使用字符串初始化
- (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对象,常用于文字图片混排 |
NSStrikethroughStyleAttributeName
和NSUnderlineStyleAttributeName
样式
样式 | 说明 |
---|---|
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])];
显示如下