iosIOS开发很常

NSAttributedString

2021-04-16  本文已影响0人  仲子

字面意思,一个带属性的字符串。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];
WeChat69e6c1da21a702215acf645d2589a2ad.png

2、NSParagraphStyleAttributeName 文本段落排版
在使用此属性前需要先了解NSMutableParagraphStyle,这个可以理解为一个配置工具类,用来配置段落样式,先讲一下NSMutableParagraphStyle的所有属性。

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)];
WeChat0503b5d4363608bcb0c6b31d880a042f.png

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];
WeChata42e854d43169081d25e20ed41c03512.png

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];
WeChatf1b816be55da8bfa5a2c59dd46e19ed0.png

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)];

注意下图部分个别变化


WeChat17fd6b4b6e31029a13ee042bdf19a19f.png WeChat379c37752dbb0698b274000468c12ca3.png

6、NSKernAttributeName 文字间隔

NSString *string = @"山居秋暝\n空山新雨后 天气晚来秋\n明月松间照 清泉石上流";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
[attributeString addAttributes:@{NSKernAttributeName: @10} range:NSMakeRange(0, string.length)];
WeChatcc49f93464fd9fbb9e413131182a16c7.png

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)];
WeChat170beecb4d36f3d65ccdd08bdac22b63.png

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)];
WeChat63d29fdec287007879fada98ee11a575.png

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)];
WeChat8471f2ba9c9816ffa21c542bb600da84.png

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)];
WeChatf51813d48f8d04879cc2d5a3031413e5.png

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)];
WeChatf49e32a4d6e97316fe7b974dc23b9eee.png

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)];
WeChat607042be664bbcca74884079e1a865f3.png

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];
WeChat2c0e0010f0b8b546a1b2d57b864c7e83.png

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];
WeChatf5b5cedb54cf45efae746a6f566bec14.png

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];
WeChat47781d737c74fcc42513a380087f5ceb.png

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];
WeChatad9f41ed74c498a2a74f2dd2220d5bc8.png

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];
WeChatcefc673d389c2cda6f6b88252a960c7b.png

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];
WeChatb83a047ef7560ed6ab2f0ad1d4bc83dc.png

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];
WeChat02dde67ed74a5b742fe1918536eb9f19.png

22、NSVerticalGlyphFormAttributeName 文字排版方向
value为NSNumber类型,0表示水平,1表示垂直,但在iOS中,只能以横向排版

上一篇下一篇

猜你喜欢

热点阅读