iOS利用NSMutableAttributedString實現(xiàn)富文本的方法小結(jié)
前言
在iOS開發(fā)過程中,經(jīng)常會用到給字體加下劃線,顯示不同顏色和大小的字體等需求,經(jīng)常遇到這種需求都是直接到百度或者谷歌直接把代碼粘過來,并沒有做系統(tǒng)的整理,今天剛好有時間,把這部分的內(nèi)容整理一下,便于后續(xù)的開發(fā),閑話不說,接下來就跟著我一起來了解一下NSMutableAttributedString吧.
NSAttributedString
NSAttributedString對象管理適用于字符串中單個字符或字符范圍的字符串和關(guān)聯(lián)的屬性集(例如字體和字距)。NSAttributedString對象的默認(rèn)字體是Helvetica 12點,可能與平臺的默認(rèn)系統(tǒng)字體不同。因此,您可能希望創(chuàng)建適用于您的應(yīng)用程序的非默認(rèn)屬性的新字符串。您還可以使用NSParagraphStyle類及其子類NSMutableParagraphStyle來封裝NSAttributedString類使用的段落或標(biāo)尺屬性。
實例化方法和使用方法
實例化方法
使用字符串初始化
- (instancetype)initWithString:(NSString *)str;
代碼示例
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"測試數(shù)據(jù)"];
字典中存放一些屬性名和屬性值
- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs;
代碼示例
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"測試數(shù)據(jù)" attributes:attributedDict];
使用NSAttributedString初始化,與NSMutableString,NSString類似
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
使用方法
為某一范圍內(nèi)的文字設(shè)置多個屬性的方法
- (void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;
//代碼示例
NSString *string = @"測試數(shù)據(jù)";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString setAttributes:attributedDict range:NSMakeRange(0, string.length)];
為某一范圍內(nèi)的文字添加某個屬性的方法
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//代碼示例
NSString *string = @"測試數(shù)據(jù)"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)];
為某一范圍內(nèi)的文字添加多個屬性的方法
- (void)addAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;
//代碼示例
NSString *string = @"測試數(shù)據(jù)";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];
移除某個范圍內(nèi)的某個屬性的方法
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
//代碼示例
NSString *string = @"測試數(shù)據(jù)";
NSDictionary *attributedDict = @{
NSFontAttributeName:[UIFont systemFontOfSize:16.0],
NSForegroundColorAttributeName:[UIColor redColor],
NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick)
};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
[attributedString addAttributes:attributedDict range:NSMakeRange(0, string.length)];
[attributedString removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(0, string.length)];
屬性及說明
| key | 說明 |
|---|---|
| NSFontAttributeName | 字體,value是UIFont對象 |
| NSParagraphStyleAttributeName | 繪圖的風(fēng)格(居中,換行模式,間距等諸多風(fēng)格),value是NSParagraphStyle對象 |
| NSForegroundColorAttributeName | 文字顏色,value是UIFont對象 |
| NSLigatureAttributeName | 字符連體,value是NSNumber |
| NSKernAttributeName | 字符間隔 |
| NSStrikethroughStyleAttributeName | 刪除線,value是NSNumber |
| NSUnderlineStyleAttributeName | 下劃線,value是NSNumber |
| NSStrokeColorAttributeName | 描繪邊顏色,value是UIColor |
| NSStrokeWidthAttributeName | 描邊寬度,value是NSNumber |
| NSShadowAttributeName | 陰影,value是NSShadow對象 |
| NSTextEffectAttributeName | 文字效果,value是NSString |
| NSAttachmentAttributeName | 附屬,value是NSTextAttachment 對象 |
| NSLinkAttributeName | 鏈接,value是NSURL or NSString |
| NSBaselineOffsetAttributeName | 基礎(chǔ)偏移量,value是NSNumber對象 |
| NSStrikethroughColorAttributeName | 刪除線顏色,value是UIColor |
| NSObliquenessAttributeName | 字體傾斜 |
| NSExpansionAttributeName | 字體扁平化 |
| NSVerticalGlyphFormAttributeName | 垂直或者水平,value是 NSNumber,0表示水平,1垂直 |
富文本段落排版格式屬性說明
| 屬性 | 說明 |
|---|---|
| lineSpacing | 字體的行間距 |
| firstLineHeadIndent | 首行縮進 |
| alignment | (兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,自然) |
| lineBreakMode | 結(jié)尾部分的內(nèi)容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz") |
| headIndent | 整體縮進(首行除外) |
| minimumLineHeight | 最低行高 |
| maximumLineHeight | 最大行高 |
| paragraphSpacing | 段與段之間的間距 |
| paragraphSpacingBefore | 段首行空白空間 |
| baseWritingDirection | 書寫方向(一共三種) |
| hyphenationFactor | 連字屬性 在iOS,唯一支持的值分別為0和1 |
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
在iOS應(yīng)用中使用UIWebView創(chuàng)建簡單的網(wǎng)頁瀏覽器界面
這篇文章主要介紹了在iOS應(yīng)用中使用UIWebView創(chuàng)建簡單的網(wǎng)頁瀏覽器界面的方法,包括動態(tài)獲取UIWebView高度的實現(xiàn),需要的朋友可以參考下2016-01-01
iOS中管理剪切板的UIPasteboard粘貼板類用法詳解
在iOS中,通過UITextField、UITextView和UIWebView剪切或復(fù)制的內(nèi)容都可以通過UIPasteboard類來管理粘貼操作,下面就為大家?guī)韎OS中管理剪切板的UIPasteboard粘貼板類用法詳解:2016-06-06
iOS實現(xiàn)漸變按鈕Gradient Button的方法示例
這篇文章主要給大家介紹了關(guān)于iOS實現(xiàn)漸變按鈕Gradient Button的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
iOS開發(fā)中Quartz2D控制圓形縮放和實現(xiàn)刷幀效果
這篇文章主要介紹了iOS開發(fā)中Quartz2D控制圓形縮放和實現(xiàn)刷幀效果的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12

