NSAttributedString共有21種效果
核心API:
NSAttributedString
NSMutableAttributedString
三種初始化方法:
NSAttributedString
1、initWithString:
2、initWithString:attributes:
3、initWithAttributedString:
NSMutableAttributedString沒有初始化方法,使用父類初始化方法
| 序號 | 屬性 | 作用 | 類型 | 備注 |
|---|---|---|---|---|
| 1 | NSFontAttributeName | 設(shè)置字體屬性 | UIFont | 默認值:字體:Helvetica(Neue) 字號:12 |
| 2 | NSForegroundColorAttributeName | 設(shè)置字體顏色 | UIColor | 默認值為黑色 |
| 3 | NSBackgroundColorAttributeName | 設(shè)置字體所在區(qū)域背景顏色 | 取值為 UIColor對象 | 默認值為nil |
| 4 | NSLigatureAttributeName | 設(shè)置連體屬性 | NSNumber | 0 表示沒有連體字符,1 表示使用默認的連體字符,一般中文用不到,在英文中可能出現(xiàn)相鄰字母連筆的情況 |
| 5 | NSKernAttributeName | 設(shè)置字符間距 | NSNumber | 正值間距加寬,負值間距變窄,0表示禁用了字距調(diào)整。 |
| 6 | NSParagraphStyleAttributeName | 設(shè)置文本段落排版格式 | NSParagraphStyle | 默認defaultParagraphStyle |
| 7 | NSStrikethroughStyleAttributeName | 刪除線 | NSNumber | 默認0:無刪除 |
| 8 | NSStrikethroughColorAttributeName | 設(shè)置刪除線顏色 | UIColor | 默認值為黑色 |
| 9 | NSUnderlineStyleAttributeName | 設(shè)置下劃線 | NSNumber | NSUnderlineStyle中的值,與刪除線類似,默認0:無下劃線 |
| 10 | NSUnderlineColorAttributeName | 設(shè)置下劃線顏色 | UIColor | 默認值為黑色 |
| 11 | NSStrokeWidthAttributeName | 設(shè)置筆畫寬度(粗細) | NSNumber | 負值填充效果,正值中空效果,(概述文本的典型值為3.0) |
| 12 | NSStrokeColorAttributeName | 填充部分顏色,不是字體顏色 | UIColor | 默認nil |
| 13 | NSShadowAttributeName | 設(shè)置陰影屬性 | NSShadow | |
| 14 | NSTextEffectAttributeName | 設(shè)置文本特殊效果 | NSString | 默認nil:無文本效果,目前只有圖版印刷效果可用 |
| 15 | NSBaselineOffsetAttributeName | 設(shè)置基線偏移值 | NSNumber | 默認為0,正值上偏,負值下偏 |
| 16 | NSObliquenessAttributeName | 設(shè)置字形傾斜度 | NSNumber | 正值右傾,負值左傾,默認0:無傾斜 |
| 17 | NSExpansionAttributeName | 設(shè)置文本橫向拉伸屬性 | NSNumber | 正值橫向拉伸文本,負值橫向壓縮文本,默認0:沒有拉伸 |
| 18 | NSWritingDirectionAttributeName | 設(shè)置文字書寫方向 | NSNumber | 從左向右書寫或者從右向左書寫 |
| 19 | NSVerticalGlyphFormAttributeName | 設(shè)置文字排版方向 | NSNumber | 0 表示橫排文本,1 表示豎排文本,目前在iOS上,它總是水平的。任何其他值的行為都是未定義的。 |
| 20 | NSAttachmentAttributeName | 設(shè)置文本附件 | NSTextAttachment | 常用于文字圖片混排,默認nil |
| 21 | NSLinkAttributeName | 設(shè)置鏈接屬性,點擊后調(diào)用瀏覽器打開指定URL地址 | NSURL | 點擊后調(diào)用瀏覽器打開指定URL地址(適用于UITextView) |
1. NSFontAttributeName -> 設(shè)置字體屬性,默認值:字體:Helvetica(Neue) 字號:12
NSAttributedString *att1 = [[NSAttributedString alloc] initWithString:@"01-設(shè)置字體大小-默認字體" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}];
att1 = [[NSAttributedString alloc] initWithString:@"01-設(shè)置字體大小-粗體" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:15]}];
att1 = [[NSAttributedString alloc] initWithString:@"01-設(shè)置字體大小-斜體" attributes:@{NSFontAttributeName : [UIFont italicSystemFontOfSize:15]}];
2. NSForegroundColorAttributeName ->設(shè)置字體顏色,取值為 UIColor對象,默認值為黑色
NSAttributedString *att2 = [[NSAttributedString alloc] initWithString:@"02-設(shè)置字體顏色" attributes:@{NSForegroundColorAttributeName : [UIColor redColor]}];
3. NSBackgroundColorAttributeName ->設(shè)置字體所在區(qū)域背景顏色,取值為 UIColor對象,默認值為nil, 透明色
NSAttributedString *att3 = [[NSAttributedString alloc] initWithString:@"03-設(shè)置背景顏色" attributes:@{NSBackgroundColorAttributeName : [UIColor redColor]}];
4. NSLigatureAttributeName ->設(shè)置連體屬性,取值為NSNumber 對象(整數(shù)),0 表示沒有連體字符,1 表示使用默認的連體字符,一般中文用不到,在英文中可能出現(xiàn)相鄰字母連筆的情況
NSAttributedString *att4 = [[NSAttributedString alloc] initWithString:@"04-設(shè)置連體屬性abcdefghijkl" attributes:@{NSLigatureAttributeName : @1}];
5. NSKernAttributeName ->設(shè)置字符間距,取值為 NSNumber 對象(整數(shù)),正值間距加寬,負值間距變窄,0表示禁用了字距調(diào)整。
NSAttributedString *att5 = [[NSAttributedString alloc] initWithString:@"5-設(shè)置字符間距" attributes:@{NSKernAttributeName : @10}];
6. NSParagraphStyleAttributeName ->設(shè)置文本段落排版格式(包含字間距、行間距等屬性),取值為 NSParagraphStyle 對象(詳情見下面的API說明),默認defaultParagraphStyle
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10; // 字體的行間距
// paragraphStyle.firstLineHeadIndent = 20.0f; // 首行縮進
// paragraphStyle.alignment = NSTextAlignmentJustified; //(兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,自然)
// paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; // 結(jié)尾部分的內(nèi)容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
// paragraphStyle.headIndent = 20; // 整體縮進(首行除外)
// paragraphStyle.tailIndent = 20; //
// paragraphStyle.minimumLineHeight = 10; // 最低行高
// paragraphStyle.maximumLineHeight = 20; // 最大行高
// paragraphStyle.paragraphSpacing = 15; // 段與段之間的間距
// paragraphStyle.paragraphSpacingBefore = 22.0f; // 段首行空白空間
// paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;// 從左到右的書寫方向
// paragraphStyle.lineHeightMultiple = 15;// 在受到最小線高和最大線高的約束之前,將自然線高乘以這個因子(如果為正)。
// paragraphStyle.hyphenationFactor = 1; // 連字屬性 在iOS,唯一支持的值分別為0和1
NSAttributedString *att6 = [[NSAttributedString alloc] initWithString:@"06-設(shè)置排版樣式(包含字間距、行間距等屬性)-設(shè)置排版樣式(包含字間距、行間距等屬性)-設(shè)置排版樣式(包含字間距、行間距等屬性)-設(shè)置排版樣式(包含字間距、行間距等屬性)" attributes:@{NSParagraphStyleAttributeName:paragraphStyle}];
7. NSStrikethroughStyleAttributeName ->設(shè)置刪除線,取值為 NSNumber 對象(整數(shù)),默認0:無刪除
NSAttributedString *att7 = [[NSAttributedString alloc] initWithString:@"7-設(shè)置刪除線" attributes:@{NSStrikethroughStyleAttributeName : @3}];
8. NSStrikethroughColorAttributeName ->設(shè)置刪除線顏色,取值為 UIColor 對象,默認值為黑色
NSAttributedString *att8 = [[NSAttributedString alloc] initWithString:@"8-設(shè)置刪除線顏色" attributes:@{NSStrikethroughStyleAttributeName : @3,NSStrikethroughColorAttributeName:[UIColor redColor]}];
9. NSUnderlineStyleAttributeName ->設(shè)置下劃線,取值為 NSNumber 對象(整數(shù)),枚舉常量 NSUnderlineStyle中的值,與刪除線類似,默認0:無下劃線
NSAttributedString *att9 = [[NSAttributedString alloc] initWithString:@"09-設(shè)置下劃線" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)}];
10. NSUnderlineColorAttributeName ->設(shè)置下劃線顏色,取值為 UIColor 對象,默認值為黑色
NSAttributedString *att10 = [[NSAttributedString alloc] initWithString:@"10-設(shè)置下劃線顏色" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle),NSUnderlineColorAttributeName:[UIColor redColor]}];
11. NSStrokeWidthAttributeName ->設(shè)置筆畫寬度(粗細),取值為 NSNumber 對象(整數(shù)),負值填充效果,正值中空效果,(概述文本的典型值為3.0)
NSAttributedString *att11 = [[NSAttributedString alloc] initWithString:@"11-設(shè)置畫筆顏色" attributes:@{NSStrokeWidthAttributeName:@5}];
12. NSStrokeColorAttributeName ->填充部分顏色,不是字體顏色,取值為 UIColor 對象,默認nil
NSAttributedString *att12 = [[NSAttributedString alloc] initWithString:@"12-設(shè)置畫筆顏色" attributes:@{NSStrokeWidthAttributeName:@5,NSStrokeColorAttributeName:[UIColor redColor]}];
13. NSShadowAttributeName ->設(shè)置陰影屬性,取值為 NSShadow 對象
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(5, 5);
shadow.shadowColor = [UIColor greenColor];
NSAttributedString *att13 = [[NSAttributedString alloc] initWithString:@"13-設(shè)置陰影屬性" attributes:@{NSShadowAttributeName:shadow}];
14. NSTextEffectAttributeName ->設(shè)置文本特殊效果,取值為 NSString 對象,默認nil:無文本效果,目前只有圖版印刷效果可用
NSAttributedString *att14 = [[NSAttributedString alloc] initWithString:@"14-設(shè)置文本特殊效果" attributes:@{NSTextEffectAttributeName:NSTextEffectLetterpressStyle}];
15. NSBaselineOffsetAttributeName ->設(shè)置基線偏移值,取值為 NSNumber (float),默認為0,正值上偏,負值下偏
NSAttributedString *att15 = [[NSAttributedString alloc] initWithString:@"15-設(shè)置基線偏移量" attributes:@{NSBaselineOffsetAttributeName:@15}];
16. NSObliquenessAttributeName ->設(shè)置字形傾斜度,取值為 NSNumber (float),正值右傾,負值左傾,默認0:無傾斜
NSAttributedString *att16 = [[NSAttributedString alloc] initWithString:@"16-設(shè)置基線偏移量" attributes:@{NSObliquenessAttributeName:@0.8}];
17. NSExpansionAttributeName ->設(shè)置文本橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本,默認0:沒有展開
NSAttributedString *att17 = [[NSAttributedString alloc] initWithString:@"17-設(shè)置文本橫向拉伸屬性" attributes:@{NSExpansionAttributeName:@1.0}];
18. NSWritingDirectionAttributeName ->設(shè)置文字書寫方向,從左向右書寫或者從右向左書寫
NSAttributedString *att18 = [[NSAttributedString alloc] initWithString:@"18-設(shè)置文本書寫方向" attributes:@{NSWritingDirectionAttributeName:@[@3]}];
19. NSVerticalGlyphFormAttributeName ->設(shè)置文字排版方向,取值為 NSNumber 對象(整數(shù)),0 表示橫排文本,1 表示豎排文本,目前在iOS上,它總是水平的。任何其他值的行為都是未定義的。
NSAttributedString *att19 = [[NSAttributedString alloc] initWithString:@"19-設(shè)置文本排版方向" attributes:@{NSVerticalGlyphFormAttributeName:@1}];
20. NSAttachmentAttributeName ->設(shè)置文本附件,取值為NSTextAttachment對象,常用于文字圖片混排,默認nil
NSMutableAttributedString * att20 = [[NSMutableAttributedString alloc] init];
NSAttributedString *att = [[NSAttributedString alloc] initWithString:@"20-設(shè)置文本附件"];
[att20 appendAttributedString:att];
// 設(shè)置圖片
NSTextAttachment *attach = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
attach.bounds = CGRectMake(0, 0, 15, 15);
attach.image = [UIImage imageNamed:@"guanli"];
NSAttributedString *strAtt = [NSAttributedString attributedStringWithAttachment:attach];
[att20 appendAttributedString:strAtt];
21. NSLinkAttributeName ->設(shè)置鏈接屬性,點擊后調(diào)用瀏覽器打開指定URL地址(適用于UITextView)
NSString *strLink =@" 21-設(shè)置鏈接屬性";
NSAttributedString *att21 = [[NSAttributedString alloc] initWithString:strLink attributes:@{NSLinkAttributeName: [NSURL URLWithString:@"http://www.baidu.com"]}];