今天突然想到之前用到的富文本用到的屬性太少,想完整的總結下全部的屬性以備用,所以想著抽點時間來整理下.
富文本的用法
首先,先看一個簡單富文本的例子,了解一下富文本的用法。
** 下面是代碼:**
/* 將需要的富文本屬性放入字典 */NSDictionary*attributes = @{NSForegroundColorAttributeName:[UIColorredColor],NSBackgroundColorAttributeName:[UIColorgreenColor],NSFontAttributeName:[UIFontfontWithName:@"TimesNewRomanPS-BoldItalicMT"size:19.0],NSKernAttributeName:@1.0};
/* 創建富文本對象 */NSAttributedString*attributeText = [[NSAttributedStringalloc] initWithString:@"This is an attributes string"attributes:attributes];
/* 設置 UILabel 的富文本 */UILabel*label = [[UILabelalloc] initWithFrame:CGRectMake(0,0,500,40)];[label setAttributedText:attributeText];[self.view addSubview:label];
** 效果圖:**
用法很簡單,就是先將富文本屬性都放在一個?NSDictionary?字典中,然后定義一個?NSAttributedString?,設置文字和富文本屬性,然后設置?UILabel?的?AttributedText屬性就可以了
富文本的屬性
用法就是上面說的那樣,下面主要開始介紹放在?NSDictionary?字典中的不同富文本屬性的用法及效果。
** 下面是系統提供的所有富文本屬性:**
/** 系統提供的所有富文本屬性 */
NSFontAttributeName// 設置字體
NSParagraphStyleAttributeName// 設置段落風格
NSForegroundColorAttributeName// 設置文字顏色
NSBackgroundColorAttributeName// 設置背景顏色
NSLigatureAttributeName// 設置連體屬性
NSKernAttributeName// 設置字符間距
NSStrikethroughStyleAttributeName// 添加刪除線
NSUnderlineStyleAttributeName// 添加下劃線
NSStrokeColorAttributeName// 設置文字描邊顏色
NSStrokeWidthAttributeName// 設置文字描邊寬度
NSShadowAttributeName// 設置陰影
NSTextEffectAttributeName// 設置文本特殊效果
NSAttachmentAttributeName// 設置文本附件
NSLinkAttributeName// 設置鏈接屬性
NSBaselineOffsetAttributeName// 設置基線偏移量
NSUnderlineColorAttributeName// 添加下劃線顏色
NSStrikethroughColorAttributeName// 添加刪除線顏色
NSObliquenessAttributeName// 設置字體傾斜
NSExpansionAttributeName// 設置文本扁平
NSWritingDirectionAttributeName// 設置文字書寫方向
NSVerticalGlyphFormAttributeName// 設置文本段落排版格式
下面是詳細的說明:
1. NSFontAttributeName 設置字體
NSFontAttributeName:[UIFontsystemFontOfSize:(CGFloat)]// 字體大小NSFontAttributeName:[UIFontfontWithName:(nonnullNSString*) size:(CGFloat)]// 字體名稱,字體大小
2. NSParagraphStyleAttributeName 設置段落風格
/* 需要一個 NSMutableParagraphStyle 實例對象。 */
NSMutableParagraphStyle*paragraph = [[NSMutableParagraphStylealloc] init];
paragraph.alignment =NSTextAlignmentCenter;// 居中
paragraph.lineSpacing =10;// 文字的行間距
/* 用法 */
NSParagraphStyleAttributeName: paragraph
3. NSForegroundColorAttributeName、NSBackgroundColorAttributeName 設置字體和背景顏色
NSForegroundColorAttributeName:[UIColor redColor]
NSBackgroundColorAttributeName:[UIColor greenColor]
4. NSKernAttributeName 設置字符間距
NSKernAttributeName:@-1.0// 正值間距加寬,負值間距變窄
5. NSStrikethroughStyleAttributeName、NSUnderlineStyleAttributeName 添加刪除線和下劃線
NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)// 刪除線樣式NSStrikethroughColorAttributeName:[UIColorredColor]// 刪除線顏色
NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)// 下劃線樣式NSUnderlineColorAttributeName:[UIColorredColor]// 下劃線顏色
6. NSStrokeColorAttributeName、NSStrokeWidthAttributeName 設置文字描邊顏色和寬度
/* 單獨設置顏色無效果,需要和寬度同時設置才有效 */
NSStrokeColorAttributeName:[UIColorredColor]
NSStrokeWidthAttributeName:@3
7. NSShadowAttributeName 設置陰影
/* 需要一個 NSShadow 實例對象。 */
NSShadow* shadow = [[NSShadowalloc]init];
shadow.shadowBlurRadius =5;// 模糊度
shadow.shadowColor = [UIColorgrayColor];// 顏色
shadow.shadowOffset =CGSizeMake(1,3);// 偏移
/* 用法 */
NSShadowAttributeName:shadow
** 效果圖:**
8. NSVerticalGlyphFormAttributeName、NSObliquenessAttributeName、NSExpansionAttributeName 繪制文本、設置字體傾斜、設置文本橫向拉伸壓縮屬性
/* 對于 NSVerticalGlyphFormAttributeName 設置文本排版格式,0 表示橫排文本、1 表示豎排文本。在 iOS 中,總是使用橫排文本,0 以外的值都未定義。 */
NSVerticalGlyphFormAttributeName:@(0)// 文本排版格式
NSObliquenessAttributeName:@1// 字體傾斜
NSExpansionAttributeName:@1// 文本橫向拉伸壓縮屬性
9. NSLigatureAttributeName 設置連體屬性
/* 0 表示沒有連體字符,1 表示使用默認的連體字符,2 表示使用所有連體符號,默認值為 1(iOS 不支持 2) */
NSLigatureAttributeName:@0
NSLigatureAttributeName:@1
** 效果圖:**
10. NSTextEffectAttributeName 設置文本特殊效果
/* 取值為 NSString 對象,目前只有圖版印刷效果可用。*/
NSTextEffectAttributeName:NSTextEffectLetterpressStyle
** 效果圖:**
11. NSLinkAttributeName 設置鏈接屬性
/* 設置文字點擊跳轉的網址,點擊后調用瀏覽器打開指定 URL 地址。 */
NSLinkAttributeName:[NSURLURLWithString:@"http://www.baidu.com"]
12. NSBaselineOffsetAttributeName 設置基線偏移量
/* 正值上偏,負值下偏。 */
NSBaselineOffsetAttributeName:@3
13. NSAttachmentAttributeName 設置文本附件
/* 需要一個 NSTextAttachment 實例對象。 */
NSTextAttachment*textAttachment = [[NSTextAttachmentalloc] initWithData:(nullableNSData*) ofType:(nullableNSString*)];
/* 用法 */
NSAttachmentAttributeName:textAttachment
14. NSWritingDirectionAttributeName 設置文字書寫方向
/* 從左到右書寫 */
@[@(NSWritingDirectionLeftToRight|NSTextWritingDirectionEmbedding)]@[@(NSWritingDirectionLeftToRight|NSTextWritingDirectionOverride)]
/* 從右到左書寫 */
@[@(NSWritingDirectionRightToLeft|NSTextWritingDirectionEmbedding)]@[@(NSWritingDirectionRightToLeft|NSTextWritingDirectionOverride)]
/* 用法 */
NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft|NSWritingDirectionOverride)]
** 效果圖:**
好了,大概就是那么多,有需要補充的后續還會再補充
一點點的積累才會更加成就你,千里之行始于足下,未來的你會更加優秀