iOS學習之NSAttributedString(富文本),nsattributedstring
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];
以上是部分方法的使用,大家有興趣的可以自己試試其他的方法。