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