標(biāo)簽:
技術(shù)分享
以前看到這種字號(hào)和顏色不一樣的字符串雁刷,想出個(gè)討巧的辦法就是“¥150”一個(gè)UILable桂对,“元/位”一個(gè)UILable媳纬。今天翻看以前的工程,command點(diǎn)進(jìn)UITextField中看到[attributedText]這個(gè)關(guān)鍵字杉适,以前都沒注意過UITextField還有這個(gè)屬性,其實(shí)UITextView柳击、UILable也有這個(gè)屬性猿推,iOS6就已經(jīng)有了,說來慚愧捌肴,對(duì)此罰站1秒鐘蹬叭。
NSAttributedString叫做富文本,是一種帶有屬性的字符串状知,通過它可以輕松的在一個(gè)字符串中表現(xiàn)出多種字體秽五、字號(hào)、字體大小等各不相同的風(fēng)格饥悴,還可以對(duì)段落進(jìn)行格式化坦喘。
通過以下代碼即可實(shí)現(xiàn)上面圖示效果,十分方便西设,從此再也不用設(shè)置兩個(gè)UILable瓣铣,并且處心積慮的處理它們的長(zhǎng)度了。
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? ? //富文本對(duì)象
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”進(jìn)行分段贷揽,此設(shè)置必須在lable.numberOfLines = 0時(shí)有效棠笑,value通過NSMutableParagraphStyle設(shè)置,它有以下屬性)
[段落樣式-插曲]
1 @property(readwrite) CGFloat lineSpacing; //行間距
2 @property(readwrite) CGFloat paragraphSpacing; //段間距
3 @property(readwrite) NSTextAlignment alignment; //對(duì)齊方式
4 @property(readwrite) CGFloat firstLineHeadIndent; //首行縮緊
5 @property(readwrite) CGFloat headIndent; //除首行之外其他行縮進(jìn)
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;