字符屬性可以應(yīng)用于 attributed string 的文本中秘噪。
NSString *const NSFontAttributeName;(字體)
NSString *const NSParagraphStyleAttributeName;(段落)
NSString *const NSForegroundColorAttributeName;(字體顏色)
NSString *const NSBackgroundColorAttributeName;(字體背景色)
NSString *const NSLigatureAttributeName;(連字符)
NSString *const NSKernAttributeName;(字間距)
NSString *const NSStrikethroughStyleAttributeName;(刪除線)
NSString *const NSUnderlineStyleAttributeName;(下劃線)
NSString *const NSStrokeColorAttributeName;(邊線顏色)
NSString *const NSStrokeWidthAttributeName;(邊線寬度)
NSString *const NSShadowAttributeName;(陰影)(橫豎排版)
NSString *const NSVerticalGlyphFormAttributeName;
1. 設(shè)置Label首行縮進(jìn)
NSString * string = @"設(shè)置Label首行縮進(jìn)";
if (string.length > 140) {
self.label.text = [NSString stringWithFormat:@"%@....",[string substringToIndex:140]];
}else {
self.label.text = string;
}
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString: self.label.text];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.alignment = NSTextAlignmentJustified;// 解決UILabel文字排列左右間距不齊以及右邊不齊 或同時(shí) NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]
// style.headIndent = 0; //縮進(jìn)
style.firstLineHeadIndent = 30;
style.lineSpacing = 6; //行距
style.alignment = NSTextAlignmentLeft;
// 需要設(shè)置的范圍
NSRange range = NSMakeRange(0, self.label.text.length);
[text addAttribute:NSParagraphStyleAttributeName value:style range:range];
self.label.attributedText = text;
2. label文字間距如何調(diào)整
- (void)configureContentLabelText{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Label"];
long number = 5;
CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
[attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedString length])];
self.label.attributedText = attributedString;
}
3.添加中劃線:
NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:textStr attributes:attribtDic];
4.添加下劃線:
NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:textStr attributes:attribtDic];
常量
1> NSFontAttributeName(字體)
該屬性所對(duì)應(yīng)的值是一個(gè) UIFont 對(duì)象。該屬性用于改變一段文本的字體计雌。如果不指定該屬性,則默認(rèn)為12-point Helvetica(Neue)训唱。
2> NSParagraphStyleAttributeName(段落)
該屬性所對(duì)應(yīng)的值是一個(gè) NSParagraphStyle 對(duì)象褥蚯。該屬性在一段文本上應(yīng)用多個(gè)屬性。如果不指定該屬性况增,則默認(rèn)為 NSParagraphStyle 的defaultParagraphStyle 方法返回的默認(rèn)段落屬性赞庶。
3> NSForegroundColorAttributeName(字體顏色)
該屬性所對(duì)應(yīng)的值是一個(gè) UIColor 對(duì)象。該屬性用于指定一段文本的字體顏色澳骤。如果不指定該屬性歧强,則默認(rèn)為黑色。
4> NSBackgroundColorAttributeName(字體背景色)
該屬性所對(duì)應(yīng)的值是一個(gè) UIColor 對(duì)象为肮。該屬性用于指定一段文本的背景顏色摊册。如果不指定該屬性,則默認(rèn)無(wú)背景色弥锄。
5> NSLigatureAttributeName(連字符)
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))丧靡。連體字符是指某些連在一起的字符,它們采用單個(gè)的圖元符號(hào)籽暇。0 表示沒(méi)有連體字符温治。1 表示使用默認(rèn)的連體字符。2表示使用所有連體符號(hào)戒悠。默認(rèn)值為 1(注意熬荆,iOS 不支持值為 2)。
6> NSKernAttributeName(字間距)
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))绸狐。字母緊排指定了用于調(diào)整字距的像素點(diǎn)數(shù)卤恳。字母緊排的效果依賴于字體。值為 0 表示不使用字母緊排寒矿。默認(rèn)值為0突琳。
7> NSStrikethroughStyleAttributeName(刪除線)
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))。該值指定是否在文字上加上刪除線符相,該值參考“Underline Style Attributes”拆融。默認(rèn)值是NSUnderlineStyleNone。
8> NSUnderlineStyleAttributeName(下劃線)
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))啊终。該值指定是否在文字上加上下劃線镜豹,該值參考“Underline Style Attributes”。默認(rèn)值是NSUnderlineStyleNone蓝牲。
9> NSStrokeColorAttributeName(邊線顏色)
該屬性所對(duì)應(yīng)的值是一個(gè) UIColor 對(duì)象趟脂。如果該屬性不指定(默認(rèn)),則等同于 NSForegroundColorAttributeName例衍。否則昔期,指定為刪除線或下劃線顏色已卸。更多細(xì)節(jié)見(jiàn)“Drawing attributedstrings that are both filled and stroked”。
10> NSStrokeWidthAttributeName(邊線寬度)
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(小數(shù))镇眷。該值改變描邊寬度(相對(duì)于字體size 的百分比)咬最。默認(rèn)為 0,即不改變欠动。正數(shù)只改變描邊寬度永乌。負(fù)數(shù)同時(shí)改變文字的描邊和填充寬度。例如具伍,對(duì)于常見(jiàn)的空心字翅雏,這個(gè)值通常為3.0。
11> NSShadowAttributeName(陰影)
該屬性所對(duì)應(yīng)的值是一個(gè) NSShadow 對(duì)象人芽。默認(rèn)為 nil望几。
12> NSVerticalGlyphFormAttributeName(橫豎排版)
該屬性所對(duì)應(yīng)的值是一個(gè) NSNumber 對(duì)象(整數(shù))。0 表示橫排文本萤厅。1 表示豎排文本橄抹。在 iOS 中,總是使用橫排文本惕味,0 以外的值都未定義楼誓。