字面意思奏司,一個(gè)帶屬性的字符串伸刃。NSAttributedString為字符串提供豐富的展示效果提供了可能性谎砾,使用NSAttributedString可以很方便的實(shí)現(xiàn)在一段字符串中呈現(xiàn)不同的字體逢倍、不同的顏色捧颅、劃線、縮進(jìn)较雕、行間距碉哑、甚至圖文混排等不同的效果。
NSAttributedString提供了22種屬性的使用亮蒋,本文詳細(xì)介紹扣典。
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贮尖,這個(gè)可以理解為一個(gè)配置工具類,用來配置段落樣式趁怔,先講一下NSMutableParagraphStyle的所有屬性湿硝。
- lineSpacing: 行間距
- paragraphSpacing:段與段之間的間距
- alignment:對齊方式
- firstLineHeadIndent::首行縮進(jìn)
- headIndent:除首行外的整體縮進(jìn)
- tailIndent:右端文本縮進(jìn)
- lineBreakMode:折行方式
- minimumLineHeight:最小行高
- maximumLineHeight:最大行高
- baseWritingDirection:寫入方式
- lineHeightMultiple:默認(rèn)行高的倍數(shù)
- paragraphSpacingBefore:段首留白空間
- hyphenationFactor:設(shè)置每行的最后單詞是否截?cái)啵?.0-1.0之間润努,默認(rèn)為0.0关斜,越接近1.0單詞被截?cái)嗟目赡苄栽酱?/li>
- defaultTabInterval:默認(rèn)的tab間隔
- allowsDefaultTighteningForTruncation:行間距調(diào)整收縮時(shí)允許截?cái)啵J(rèn)為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)];
注意下圖部分個(gè)別變化
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 下劃線
蘋果給下劃線樣式定義了一個(gè)枚舉
//無效果
NSUnderlineStyleNone = 0x00,
//單行線
NSUnderlineStyleSingle = 0x01,
//加粗單行線
NSUnderlineStyleThick API_AVAILABLE(macos(10.0), ios(7.0)) = 0x02,
//雙行線
NSUnderlineStyleDouble API_AVAILABLE(macos(10.0), ios(7.0)) = 0x09,
//以下單獨(dú)使用沒效果矗积,需要結(jié)合上面屬性一起使用
//無效果
NSUnderlineStylePatternSolid API_AVAILABLE(macos(10.0), ios(7.0)) = 0x0000,
//以下虛線效果可以自己嘗試著玩一下 value設(shè)置舉例:[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乍恐,負(fù)值為描邊效果评疗,見下圖
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 特殊效果,目前蘋果只提供一個(gè)可用效果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新建一個(gè)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中使用,在代理方法中捕捉點(diǎn)擊事件
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"]) {
//點(diǎn)擊事件
}
return YES;
}
16洒扎、NSBaselineOffsetAttributeName 基線偏移辑甜,正值向上偏,負(fù)值向下偏
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中,只能以橫向排版