NSAttributedString 叫做富文本,是一種帶有屬性的字符串,通過它可以輕松的在一個字符串中表現出多種字體、字號、字體大小等各不相同的風格,還可以對段落進行格式化,一般都是對可變富文本(NSMutableAttributedString)進行一些操作
一、NSMutableAttributedString 類的部分常用方法
// 在一定范圍中添加單個文字屬性 // 參數1:字符屬性名 // 參數2:屬性值 // 參數3:范圍 - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; // 在一定范圍中使用字典添加多個文字屬性 // 參數1:屬性字典 // 參數2:范圍 - (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range; // 在一定范圍中刪除文字具有的某個文字屬性 // 參數1:字符屬性名 // 參數2:范圍 - (void)removeAttribute:(NSString *)name range:(NSRange)range; // 在一定范圍中替換字符串 // 參數1:范圍 // 參數2:要替換的字符串 - (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString; // 在對應的角標處插入富文本 // 參數1:要插入的字符串 // 參數2:要插入的角標位置 - (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc; // 將某個富文本拼接到后面 // 參數:要拼接的字符串 - (void)appendAttributedString:(NSAttributedString *)attrString; // 刪除一定范圍中的字符 // 參數:范圍 - (void)deleteCharactersInRange:(NSRange)range; // 將字符串全部置換為另一個富文本字符串 // 參數:置換后的富文本字符串 - (void)setAttributedString:(NSAttributedString *)attrString;
二、字符屬性
1.NSString *const NSFontAttributeName(字體):
該屬性所對應的值是一個 UIFont 對象。該屬性用於改變一段文本的字體。如果不指定該屬性,則默認為12-point Helvetica(Neue)。
2.NSString *const NSParagraphStyleAttributeName(段落):
該屬性所對應的值是一個 NSParagraphStyle 對 象。該屬性在一段文本上應用多個屬性。如果不指定該屬性,則默認為 NSParagraphStyle 的 defaultParagraphStyle 方法返回的默認段落屬性。想要了解NSParagraphStyle可以自行百度學習,在這里不詳細描述。注意:lable的numberOfLines 屬性必須設置為0,段落樣式才能生效。
3.NSString *const NSForegroundColorAttributeName(字體顏色):
該屬性所對應的值是一個 UIColor 對象。該屬性用於指定一段文本的字體顏色。如果不指定該屬性,則默認為黑色。
4.NSString *const NSBackgroundColorAttributeName(字體背景色):
該屬性所對應的值是一個 UIColor 對象。該屬性用於指定一段文本的背景顏色。如果不指定該屬性,則默認無背景色。
5.NSString *const NSLigatureAttributeName(連字符):
該屬性所對應的值是一個 NSNumber 對象(整數)。連體字符是指某些連在一起的字符,它們采用單個的圖元符號。0 表示沒有連體字符。1 表示使用默認的連體字符。2表示使用所有連體符號。默認值為 1(注意,iOS 不支持值為 2)。
6.NSString *const NSKernAttributeName(字間距):
該屬性所對應的值是一個 NSNumber 對象(整數)。連體字符是指某些連在一起的字符,它們采用單個的圖元符號。0 表示沒有連體字符。1 表示使用默認的連體字符。2表示使用所有連體符號。默認值為 1(注意,iOS 不支持值為 2)。
7.NSString *const NSStrikethroughStyleAttributeName(刪除線):
該屬性所對應的值是一個 NSNumber 對象(整數)。該值指定是否在文字上加上刪除線,該值參考“Underline Style Attributes”。默認值是NSUnderlineStyleNone。
8.NSString *const NSUnderlineStyleAttributeName(下划線):
該屬性所對應的值是一個 NSNumber 對象(整數)。該值指定是否在文字上加上下划線,該值參考“Underline Style Attributes”。默認值是NSUnderlineStyleNone。
9.NSString *const NSStrokeColorAttributeName(邊線顏色):
該屬性所對應的值是一個 UIColor 對象。如果該屬性不指定(默認),則等同於 NSForegroundColorAttributeName。否則,指定為刪除線或下划線顏色。更多細節見“Drawing attributedstrings that are both filled and stroked”。
10.NSString *const NSStrokeWidthAttributeName(邊線寬度):
該屬性所對應的值是一個 NSNumber 對象(小數)。該值改變描邊寬度(相對於字體size 的百分比)。默認為 0,即不改變。正數只改變描邊寬度。負數同時改變文字的描邊和填充寬度。例如,對於常見的空心字,這個值通常為3.0。
11.NSString *const NSShadowAttributeName(陰影):
該屬性所對應的值是一個 NSShadow 對象。默認為 nil。
12.NSString *const NSVerticalGlyphFormAttributeName(橫豎排版):
該屬性所對應的值是一個 NSNumber 對象(整數)。0 表示橫排文本。1 表示豎排文本。在 iOS 中,總是使用橫排文本,0 以外的值都未定義。
三、代碼示例
在這里給大家舉了幾個簡單的例子,有興趣的可以嘗試其余屬性的效果。
- 字符串沒有添加屬性
NSString *contentStr = @"Hello World!"; // 初始化屬性字符串 NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[contentStr stringByAppendingString:@"\n\n"]];
- 添加單個屬性(以字體顏色和字體大小為例)
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 6)]; [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, 12)];
- 使用屬性字典添加多個屬性
[attrStr addAttributes:@{
NSForegroundColorAttributeName : [UIColor yellowColor],
NSBackgroundColorAttributeName : [UIColor lightGrayColor]
}
range:NSMakeRange(0,6)];
- 刪除屬性
[attrStr removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(6, 3)]; [attrStr removeAttribute:NSBackgroundColorAttributeName range:NSMakeRange(5, 1)];
字符串全部替換
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"123"]; [attrStr setAttributedString:str];
以上是部分方法的使用,大家有興趣的可以自己試試其他的方法。