隨著項目的不斷迭代,普通的文本屬性已經(jīng)無法滿足需求岗憋,就需要我們學(xué)習(xí)和使用更加靈活的富文本绪颖。AttributedString 可以分為 NSAttributedString 和 NSMutableAttributedString 兩種抡笼。在使用中通過將 AttributedString 賦值給控件的 attributedText 屬性來添加文字樣式苏揣。有此屬性的控件有 UILabel、UITextField 和 UITextView推姻。
/
1平匈、 NSFontAttributeName(字體)
該屬性所對應(yīng)的值是一個 UIFont 對象。該屬性用于改變一段文本的字體。如果不指定該屬性增炭,則默認(rèn)為12-point Helvetica(Neue)忍燥。
2、 NSParagraphStyleAttributeName(段落)
該屬性所對應(yīng)的值是一個 NSParagraphStyle 對象隙姿。該屬性在一段文本上應(yīng)用多個屬性梅垄。如果不指定該屬性,
則默認(rèn)為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認(rèn)段落屬性输玷。
3队丝、 NSForegroundColorAttributeName(字體顏色)
該屬性所對應(yīng)的值是一個 UIColor 對象。該屬性用于指定一段文本的字體顏色欲鹏。如果不指定該屬性机久,則默認(rèn)為黑色。
4赔嚎、 NSBackgroundColorAttributeName(字體背景色)
該屬性所對應(yīng)的值是一個 UIColor 對象膘盖。該屬性用于指定一段文本的背景顏色。如果不指定該屬性尤误,則默認(rèn)無背景色侠畔。
5、 NSLigatureAttributeName(連字符)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))损晤。連體字符是指某些連在一起的字符软棺,
它們采用單個的圖元符號。0 表示沒有連體字符沉馆。1 表示使用默認(rèn)的連體字符码党。2表示使用所有連體符號德崭。默認(rèn)值為 1(注意斥黑,iOS 不支持值為 2)。
6眉厨、 NSKernAttributeName(字間距)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))锌奴。字母緊排指定了用于調(diào)整字距的像素點數(shù)。字母緊排的效果依賴于字體憾股。值為 0 表示不使用字母緊排鹿蜀。默認(rèn)值為0。
7服球、 NSStrikethroughStyleAttributeName(刪除線)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))茴恰。該值指定是否在文字上加上刪除線,該值參考“Underline Style Attributes”斩熊。默認(rèn)值是NSUnderlineStyleNone往枣。
8、 NSUnderlineStyleAttributeName(下劃線)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))。該值指定是否在文字上加上下劃線分冈,該值參考“Underline Style Attributes”圾另。默認(rèn)值是NSUnderlineStyleNone。
9雕沉、 NSStrokeColorAttributeName(邊線顏色)
該屬性所對應(yīng)的值是一個 UIColor 對象集乔。如果該屬性不指定(默認(rèn)),則等同于 NSForegroundColorAttributeName坡椒。
否則扰路,指定為刪除線或下劃線顏色。
10肠牲、 NSStrokeWidthAttributeName(邊線寬度)
該屬性所對應(yīng)的值是一個 NSNumber 對象(小數(shù))幼衰。該值改變描邊寬度(相對于字體size 的百分比)。默認(rèn)為 0缀雳,即不改變渡嚣。正數(shù)只改變描邊寬度。
負(fù)數(shù)同時改變文字的描邊和填充寬度肥印。例如识椰,對于常見的空心字,這個值通常為3.0深碱。
11腹鹉、 NSShadowAttributeName(陰影)
該屬性所對應(yīng)的值是一個 NSShadow 對象。默認(rèn)為 nil敷硅。
12功咒、 NSVerticalGlyphFormAttributeName(橫豎排版)
該屬性所對應(yīng)的值是一個 NSNumber 對象(整數(shù))。0 表示橫排文本绞蹦。1 表示豎排文本力奋。在 iOS 中,總是使用橫排文本幽七,0 以外的值都未定義
13景殷、NSTextEffectAttributeName ->設(shè)置文本特殊效果,取值為 NSString 對象澡屡,目前只有圖版印刷效果可用
14猿挚、 15. NSBaselineOffsetAttributeName ->設(shè)置基線偏移值,取值為 NSNumber (float),正值上偏驶鹉,負(fù)值下偏
15绩蜻、 NSObliquenessAttributeName ->設(shè)置字形傾斜度,取值為 NSNumber (float),正值右傾室埋,負(fù)值左傾
Code1
NSString *str = @"人生若只如初見办绝,何事悲風(fēng)秋畫扇踏兜。\n等閑變卻故人心,卻道故人心易變八秃。\n驪山語罷清宵半碱妆,淚雨霖鈴終不怨。\n何如薄幸錦衣郎昔驱,比翼連枝當(dāng)日愿疹尾。";
// 創(chuàng)建 NSMutableAttributedString
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
// 設(shè)置字體和設(shè)置字體的范圍
[attrStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:30.0f]
range:NSMakeRange(0, 3)];
// 添加文字顏色
[attrStr addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(17, 7)];
// 添加文字背景顏色
[attrStr addAttribute:NSBackgroundColorAttributeName
value:[UIColor orangeColor]
range:NSMakeRange(17, 7)];
// 添加下劃線
[attrStr addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
range:NSMakeRange(8, 7)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 100, 200, 0)];
label.backgroundColor = [UIColor greenColor];
// 自動換行
label.numberOfLines = 0;
// 設(shè)置label的富文本
label.attributedText = attrStr;
// label高度自適應(yīng)
[label sizeToFit];
[self.view addSubview:label];
Code2
//創(chuàng)建屬性字典
NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 15],
NSForegroundColorAttributeName: [UIColor blueColor] };
//創(chuàng)建 NSAttributedString 并賦值
_label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict]
//===============方式2=======================
UIFont *fond = [UIFont systemFontOfSize:20];
UIColor *color = [UIColor redColor];
NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setObject:fond forKey:NSFontAttributeName];
[attributes setObject:color forKey:NSForegroundColorAttributeName];
return [[NSAttributedString alloc]initWithString:text attributes:attributes];