字面意思,一个带属性的字符串。NSAttributedString为字符串提供丰富的展示效果提供了可能性,使用NSAttributedString可以很方便的实现在一段字符串中呈现不同的字体、不同的颜色、划线、缩进、行间距、甚至图文混排等不同的效果。
NSAttributedString提供了22种属性的使用,本文详细介绍。
1、NSFontAttributeName 字体属性
NSString *string = @"空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]} range:range1];
NSRange range2 = [string rangeOfString:@"清泉石上流"];
[attributeString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]} range:range2];
2、NSParagraphStyleAttributeName 文本段落排版
在使用此属性前需要先了解NSMutableParagraphStyle,这个可以理解为一个配置工具类,用来配置段落样式,先讲一下NSMutableParagraphStyle的所有属性。
- lineSpacing: 行间距
- paragraphSpacing:段与段之间的间距
- alignment:对齐方式
- firstLineHeadIndent::首行缩进
- headIndent:除首行外的整体缩进
- tailIndent:右端文本缩进
- lineBreakMode:折行方式
- minimumLineHeight:最小行高
- maximumLineHeight:最大行高
- baseWritingDirection:写入方式
- lineHeightMultiple:默认行高的倍数
- paragraphSpacingBefore:段首留白空间
- hyphenationFactor:设置每行的最后单词是否截断,在0.0-1.0之间,默认为0.0,越接近1.0单词被截断的可能性越大
- defaultTabInterval:默认的tab间隔
- allowsDefaultTighteningForTruncation:行间距调整收缩时允许截断,默认为NO
- lineBreakStrategy:换行策略
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;
paragraphStyle.alignment = NSTextAlignmentCenter;
[attributeString addAttributes:@{NSParagraphStyleAttributeName: paragraphStyle} range:NSMakeRange(0, string.length)];
3、NSForegroundColorAttributeName:文本颜色
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]} range:range1];
[attributeString addAttributes:@{NSForegroundColorAttributeName: UIColor.redColor} range:range1];
NSRange range2 = [string rangeOfString:@"清泉石上流"];
[attributeString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20]} range:range2];
[attributeString addAttributes:@{NSForegroundColorAttributeName: UIColor.cyanColor} range:range2];
4、NSBackgroundColorAttributeName:背景颜色
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20], NSBackgroundColorAttributeName: UIColor.redColor, NSForegroundColorAttributeName:UIColor.whiteColor} range:range1];
NSRange range2 = [string rangeOfString:@"清泉石上流"];
[attributeString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20], NSBackgroundColorAttributeName: UIColor.cyanColor} range:range2];
5、NSLigatureAttributeName 连体字符,不是所有字体都支持
0-不使用连体
1-使用连体
NSString *string = @"使用连体\nAction is character。 If we never did anything, we wouldn't be anybody。";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
[attributeString addAttributes:@{NSLigatureAttributeName: @1, NSFontAttributeName:[UIFont fontWithName:@"Zapfino" size:16]} range:NSMakeRange(0, string.length)];
注意下图部分个别变化
6、NSKernAttributeName 文字间隔
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
[attributeString addAttributes:@{NSKernAttributeName: @10} range:NSMakeRange(0, string.length)];
7、NSTrackingAttributeName iOS14新出的属性
效果类似NSKernAttributeName
8、NSStrikethroughStyleAttributeName 删除线
value:1-7 单行线,9-15 双行线,依次加粗
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
[attributeString addAttributes:@{NSStrikethroughStyleAttributeName: @1} range:NSMakeRange(0, string.length)];
9、NSUnderlineStyleAttributeName 下划线
苹果给下划线样式定义了一个枚举
//无效果
NSUnderlineStyleNone = 0x00,
//单行线
NSUnderlineStyleSingle = 0x01,
//加粗单行线
NSUnderlineStyleThick API_AVAILABLE(macos(10.0), ios(7.0)) = 0x02,
//双行线
NSUnderlineStyleDouble API_AVAILABLE(macos(10.0), ios(7.0)) = 0x09,
//以下单独使用没效果,需要结合上面属性一起使用
//无效果
NSUnderlineStylePatternSolid API_AVAILABLE(macos(10.0), ios(7.0)) = 0x0000,
//以下虚线效果可以自己尝试着玩一下 value设置举例:[NSNumber numberWithInteger:NSUnderlineStyleSingle | NSUnderlineStylePatternDot]
NSUnderlineStylePatternDot API_AVAILABLE(macos(10.0), ios(7.0)) = 0x0100,
NSUnderlineStylePatternDash API_AVAILABLE(macos(10.0), ios(7.0)) = 0x0200,
NSUnderlineStylePatternDashDot API_AVAILABLE(macos(10.0), ios(7.0)) = 0x0300,
NSUnderlineStylePatternDashDotDot API_AVAILABLE(macos(10.0), ios(7.0)) = 0x0400,
NSUnderlineStyleByWord API_AVAILABLE(macos(10.0), ios(7.0)) = 0x8000
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
[attributeString addAttributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:NSMakeRange(0, string.length)];
10、NSStrokeColorAttributeName 文字描边颜色,需要配合NSStrokeWidthAttributeName一起使用才有效果
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
[attributeString addAttributes:@{NSStrokeColorAttributeName:UIColor.redColor, NSStrokeWidthAttributeName: @2} range:NSMakeRange(0, string.length)];
11、NSStrokeWidthAttributeName 文字描边宽度
value为NSNumber类型,正值为镂空效果,参见NSStrokeColorAttributeName,负值为描边效果,见下图
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
[attributeString addAttributes:@{NSStrokeColorAttributeName:UIColor.redColor, NSStrokeWidthAttributeName: @-1} range:NSMakeRange(0, string.length)];
12、NSShadowAttributeName 文字阴影属性,value为NSShadow对象
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSShadow *shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(3, 3); //偏移量
shadow.shadowColor = UIColor.redColor; //颜色
shadow.shadowBlurRadius = 3; //模糊值
[attributeString addAttributes:@{NSShadowAttributeName:shadow} range:NSMakeRange(0, string.length)];
13、NSTextEffectAttributeName 特殊效果,目前苹果只提供一个可用效果NSTextEffectLetterpressStyle
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSShadow *shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(3, 3); //偏移量
shadow.shadowColor = UIColor.redColor; //颜色
shadow.shadowBlurRadius = 3; //模糊值
[attributeString addAttributes:@{NSShadowAttributeName:shadow, NSTextEffectAttributeName:NSTextEffectLetterpressStyle} range:NSMakeRange(0, string.length)];
14、NSAttachmentAttributeName 附件属性,常用于图文混排,value为NSTextAttachment对象,需要使用NSTextAttachment新建一个NSAttributedString,与文字的NSMutableAttributedString拼接才有效果
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSTextAttachment *attachment = [NSTextAttachment new];
attachment.image = [UIImage imageNamed:@"baocai"];
attachment.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:attachment];
[attributeString appendAttributedString:imageString];
15、NSLinkAttributeName 超链接,无法在label中使用,只能在TextView中使用,在代理方法中捕捉点击事件
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSFontAttributeName: [UIFont boldSystemFontOfSize:20], NSBackgroundColorAttributeName: UIColor.redColor, NSLinkAttributeName: @"click://空山新雨后"} range:range1];
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction
{
if ([URL.scheme isEqualToString:@"click"]) {
//点击事件
}
return YES;
}
16、NSBaselineOffsetAttributeName 基线偏移,正值向上偏,负值向下偏
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSBaselineOffsetAttributeName: @5, NSForegroundColorAttributeName: UIColor.redColor} range:range1];
17、NSUnderlineColorAttributeName 下划线颜色,配合NSUnderlineStyleAttributeName一起使用
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSUnderlineColorAttributeName: UIColor.redColor, NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)} range:range1];
18、NSStrikethroughColorAttributeName 删除线颜色,配合NSStrikethroughStyleAttributeName一起使用
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSStrikethroughColorAttributeName: UIColor.redColor, NSStrikethroughStyleAttributeName: @(2)} range:range1];
19、NSObliquenessAttributeName 文字倾斜属性
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSForegroundColorAttributeName: UIColor.redColor, NSObliquenessAttributeName: @(0.5)} range:range1];
20、NSExpansionAttributeName 文字横向拉伸属性
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSForegroundColorAttributeName: UIColor.redColor, NSExpansionAttributeName: @(0.5)} range:range1];
21、NSWritingDirectionAttributeName 文字书写方向
NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding
NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding
NSWritingDirectionLeftToRight|NSWritingDirectionOverride
NSWritingDirectionRightToLeft|NSWritingDirectionOverride
NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
NSRange range1 = [string rangeOfString:@"空山新雨后"];
[attributeString addAttributes:@{NSForegroundColorAttributeName: UIColor.redColor, NSWritingDirectionAttributeName: @[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)]} range:range1];
22、NSVerticalGlyphFormAttributeName 文字排版方向
value为NSNumber类型,0表示水平,1表示垂直,但在iOS中,只能以横向排版