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是UIColor對象
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



作者:coder小鵬


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM