今天就跟大家聊聊有關怎么在iOS中使用NSMutableAttributedString實現富文本,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
NSAttributedString
NSAttributedString對象管理適用于字符串中單個字符或字符范圍的字符串和關聯的屬性集(例如字體和字距)。NSAttributedString對象的默認字體是Helvetica 12點,可能與平臺的默認系統字體不同。因此,您可能希望創建適用于您的應用程序的非默認屬性的新字符串。您還可以使用NSParagraphStyle類及其子類NSMutableParagraphStyle來封裝NSAttributedString類使用的段落或標尺屬性。
實例化方法和使用方法
實例化方法
使用字符串初始化
- (instancetype)initWithString:(NSString *)str;
代碼示例
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"測試數據"];
字典中存放一些屬性名和屬性值
- (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:@"測試數據" attributes:attributedDict];使用NSAttributedString初始化,與NSMutableString,NSString類似
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
使用方法
為某一范圍內的文字設置多個屬性的方法
- (void)setAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;
//代碼示例
NSString *string = @"測試數據";
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)];為某一范圍內的文字添加某個屬性的方法
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//代碼示例
NSString *string = @"測試數據"; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, string.length)];
為某一范圍內的文字添加多個屬性的方法
- (void)addAttributes:(NSDictionary<NSString *,id> *)attrs range:(NSRange)range;
//代碼示例
NSString *string = @"測試數據";
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)];移除某個范圍內的某個屬性的方法
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
//代碼示例
NSString *string = @"測試數據";
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 | 繪圖的風格(居中,換行模式,間距等諸多風格),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 | 基礎偏移量,value是NSNumber對象 |
| NSStrikethroughColorAttributeName | 刪除線顏色,value是UIColor |
| NSObliquenessAttributeName | 字體傾斜 |
| NSExpansionAttributeName | 字體扁平化 |
| NSVerticalGlyphFormAttributeName | 垂直或者水平,value是 NSNumber,0表示水平,1垂直 |
富文本段落排版格式屬性說明
| 屬性 | 說明 |
|---|---|
| lineSpacing | 字體的行間距 |
| firstLineHeadIndent | 首行縮進 |
| alignment | (兩端對齊的)文本對齊方式:(左,中,右,兩端對齊,自然) |
| lineBreakMode | 結尾部分的內容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz") |
| headIndent | 整體縮進(首行除外) |
| minimumLineHeight | 最低行高 |
| maximumLineHeight | 最大行高 |
| paragraphSpacing | 段與段之間的間距 |
| paragraphSpacingBefore | 段首行空白空間 |
| baseWritingDirection | 書寫方向(一共三種) |
| hyphenationFactor | 連字屬性 在iOS,唯一支持的值分別為0和1 |
看完上述內容,你們對怎么在iOS中使用NSMutableAttributedString實現富文本有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。