NSFontAttributeString 的屬性
NSFontAttributeName 文字字體
NSForegroundColorAttributeName 文字前景色
NSBackgroundColorAttributeName 文字背景色
NSUnderlineStyleAttributeName 下劃線
NSUnderlineColorAttributeName 值爲UIColor類型,下劃線顏色
(void)viewDidLoad
{
[super viewDidLoad];
self.label = [[UILabel alloc] init];
//創建一個可變的富文本對象
NSString *text = @"hello,野獸";
NSMutableAttributedString *attrstring = [[NSMutableAttributedString alloc]initWithString:text];
//NSLog(@"%@",[UIFont familyNames]);
//編輯富文本對象的屬性hello,
NSDictionary *attrs = @{
NSFontAttributeName:[UIFont fontWithName:@"Zapfino" size:30],
NSForegroundColorAttributeName:[UIColor redColor],
NSBackgroundColorAttributeName:[UIColor yellowColor],
NSUnderlineStyleAttributeName:@(1),
NSUnderlineColorAttributeName:[UIColor blueColor],
};
[attrstring addAttributes:attrs range:[text rangeOfString:@"hello,"]];
//編輯富文本對象的屬性
NSDictionary *attrs2 = @{
NSFontAttributeName:[UIFont systemFontOfSize:80],
NSForegroundColorAttributeName:[UIColor blueColor],
NSBackgroundColorAttributeName:[UIColor greenColor],
};
[attrstring addAttributes:attrs2 range:[text rangeOfString:@"野獸"]];
label.attributedText = attrstring;
[self.label sizeToFit];
self.label.center = self.view.center;
[self.view addSubview:self.label];
1> NSFontAttributeName(字體)
該屬性所對應的值是一個 UIFont 對象。該屬性用於改變一段文本的字體。如果不指定該屬性,則默認爲12-point Helvetica(Neue)。
2> NSParagraphStyleAttributeName(段落)
該屬性所對應的值是一個 NSParagraphStyle 對象。該屬性在一段文本上應用多個屬性。如果不指定該屬性,則默認爲 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認段落屬性。
3> NSForegroundColorAttributeName(字體顏色)
該屬性所對應的值是一個 UIColor 對象。該屬性用於指定一段文本的字體顏色。如果不指定該屬性,則默認爲黑色。
4> NSBackgroundColorAttributeName(字體背景色)
該屬性所對應的值是一個 UIColor 對象。該屬性用於指定一段文本的背景顏色。如果不指定該屬性,則默認無背景色。
5> NSLigatureAttributeName(連字符)
該屬性所對應的值是一個 NSNumber 對象(整數)。連體字符是指某些連在一起的字符,它們採用單個的圖元符號。0 表示沒有連體字符。1 表示使用默認的連體字符。2表示使用所有連體符號。默認值爲 1(注意,iOS 不支持值爲 2)。
6> NSKernAttributeName(字間距)
該屬性所對應的值是一個 NSNumber 對象(整數)。字母緊排指定了用於調整字距的像素點數。字母緊排的效果依賴於字體。值爲 0 表示不使用字母緊排。默認值爲0。
7> NSStrikethroughStyleAttributeName(刪除線)
該屬性所對應的值是一個 NSNumber 對象(整數)。該值指定是否在文字上加上刪除線,該值參考“Underline Style Attributes”。默認值是NSUnderlineStyleNone。
8> NSUnderlineStyleAttributeName(下劃線)
該屬性所對應的值是一個 NSNumber 對象(整數)。該值指定是否在文字上加上下劃線,該值參考“Underline Style Attributes”。默認值是NSUnderlineStyleNone。
9> NSStrokeColorAttributeName(邊線顏色)
該屬性所對應的值是一個 UIColor 對象。如果該屬性不指定(默認),則等同於 NSForegroundColorAttributeName。否則,指定爲刪除線或下劃線顏色。更多細節見“Drawing attributedstrings that are both filled and stroked”。
10> NSStrokeWidthAttributeName(邊線寬度)
該屬性所對應的值是一個 NSNumber 對象(小數)。該值改變描邊寬度(相對於字體size 的百分比)。默認爲 0,即不改變。正數只改變描邊寬度。負數同時改變文字的描邊和填充寬度。例如,對於常見的空心字,這個值通常爲3.0。
11> NSShadowAttributeName(陰影)
該屬性所對應的值是一個 NSShadow 對象。默認爲 nil。
12> NSVerticalGlyphFormAttributeName(橫豎排版)
該屬性所對應的值是一個 NSNumber 對象(整數)。0 表示橫排文本。1 表示豎排文本。在 iOS 中,總是使用橫排文本,0 以外的值都未定義。