標(biāo)簽:
技術(shù)分享
以前看到這種字號和顏色不一樣的字符串订晌,想出個討巧的辦法就是“¥150”一個UILable狼电,“元/位”一個UILable蜘渣。今天翻看以前的工程菇用,command點進UITextField中看到[attributedText]這個關(guān)鍵字,以前都沒注意過UITextField還有這個屬性澜术,其實UITextView艺蝴、UILable也有這個屬性,iOS6就已經(jīng)有了鸟废,說來慚愧猜敢,對此罰站1秒鐘。
NSAttributedString叫做富文本侮攀,是一種帶有屬性的字符串锣枝,通過它可以輕松的在一個字符串中表現(xiàn)出多種字體、字號兰英、字體大小等各不相同的風(fēng)格撇叁,還可以對段落進行格式化。
通過以下代碼即可實現(xiàn)上面圖示效果畦贸,十分方便陨闹,從此再也不用設(shè)置兩個UILable,并且處心積慮的處理它們的長度了薄坏。
1 UILabel * aLable = [[UILabel alloc] initWithFrame:CGRectMake(100, 500, 200, 40)];
2 aLable.textAlignment = NSTextAlignmentCenter;
3 [self.view addSubview:aLable];
4
5 NSString * aString = @"¥150 元/位";
6
7 //富文本對象
8 NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:aString];
9
10 //富文本樣式
11 [aAttributedString addAttribute:NSForegroundColorAttributeName //文字顏色
12 value:[UIColor redColor]
13 range:NSMakeRange(0, 4)];
14
15 [aAttributedString addAttribute:NSFontAttributeName //文字字體
16 value:[UIFont systemFontOfSize:25]
17 range:NSMakeRange(0, 4)];
18
19 aLable.attributedText = aAttributedString;
常用屬性:
NSFontAttributeName 文字字體
NSParagraphStyleAttributeName 段落樣式(字符串通過“\n”進行分段趋厉,此設(shè)置必須在lable.numberOfLines = 0時有效,value通過NSMutableParagraphStyle設(shè)置胶坠,它有以下屬性)
[段落樣式-插曲]
1 @property(readwrite) CGFloat lineSpacing; //行間距
2 @property(readwrite) CGFloat paragraphSpacing; //段間距
3 @property(readwrite) NSTextAlignment alignment; //對齊方式
4 @property(readwrite) CGFloat firstLineHeadIndent; //首行縮緊
5 @property(readwrite) CGFloat headIndent; //除首行之外其他行縮進
6 @property(readwrite) CGFloat tailIndent; //每行容納字符的寬度
7 @property(readwrite) NSLineBreakMode lineBreakMode; //換行方式
8 @property(readwrite) CGFloat minimumLineHeight; //最小行高
9 @property(readwrite) CGFloat maximumLineHeight; //最大行高
10 @property(readwrite) NSWritingDirection baseWritingDirection; //書寫方式(NSWritingDirectionNatural君账,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)
11 @property(readwrite) CGFloat lineHeightMultiple;
12 @property(readwrite) CGFloat paragraphSpacingBefore;
13 @property(readwrite) float hyphenationFactor;
14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
[段落樣式demo]
1 UILabel * lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width-100, 200)];
2 lable.backgroundColor = [UIColor lightGrayColor];
3 lable.numberOfLines = 0;
4 [self.view addSubview:lable];
5
6 NSString * string = @"Always believe that something wonderful is about \nto happen沈善!";
7
8 //富文本
9 NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:string];
10
11 //段落樣式
12 NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
13
14 #warning lable.numberOfLines必須為0乡数,段落樣式才生效
15 //行間距
16 paragraphStyle.lineSpacing = 10.0;
17 //段落間距
18 paragraphStyle.paragraphSpacing = 20.0;
19
20 // paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
21 // paragraphStyle.firstLineHeadIndent = 10.0;
22 // paragraphStyle.headIndent = 50.0;
23 // paragraphStyle.tailIndent = 200.0;
24
25 [attributedString addAttribute:NSParagraphStyleAttributeName
26 value:paragraphStyle
27 range:NSMakeRange(0, string.length)];
28
29 lable.attributedText = attributedString;
技術(shù)分享
NSForegroundColorAttributeName 文字前景色
NSBackgroundColorAttributeName 文字背景色
NSLigatureAttributeName 連體字(NSNumber @0:無連體,@1:默認(rèn)連體闻牡,系統(tǒng)字體不包含對連體的支持)
NSUnderlineStyleAttributeName 下劃線
NSStrokeColorAttributeName 只有在NSStrokeWidthAttributeName設(shè)置了值之后才有效(默認(rèn)字體顏色和前景色一致净赴,如果設(shè)置的顏色和前景色不一致則前景色無效)
NSStrokeWidthAttributeName 設(shè)置該屬性之后字體變成空心字體,字體邊線寬度為value設(shè)定的值
NSBaselineOffsetAttributeName 值為NSNumber類型罩润,表明文字相對于其他文字基準(zhǔn)線向上的偏移量
NSUnderlineColorAttributeName 值為UIColor類型玖翅,下劃線顏色(只有在NSUnderlineStyleAttributeName的value為@1時有效)
NSUnderlineStyleAttributeName 值為NSNumber類型,下劃線寬度(默認(rèn)值為@0:下劃線寬度為0——不現(xiàn)實下劃線割以,@1:字符串有下劃線)