最近有個(gè)需求,要實(shí)現(xiàn)一個(gè)翻頁(yè),顯示當(dāng)前頁(yè)數(shù)和總頁(yè)數(shù),頁(yè)數(shù)和總頁(yè)數(shù)字體大小不一樣。當(dāng)然了實(shí)現(xiàn)這個(gè)效果是很容易的,兩個(gè)label簡(jiǎn)單搞定??墒?,我們使用一個(gè)label也是可以的喲。即將實(shí)現(xiàn)的效果圖如下:

Paste_Image.png
在這里我們可以使用label的一個(gè)屬性,叫做attributedText,很強(qiáng)大喲。不說(shuō)了上代碼:
//加1是為了讓page從1開(kāi)始
NSString * pageIndexStr = [NSString stringWithFormat:@"%d",(int)index+1];
NSRange range = NSMakeRange(0, pageIndexStr.length);
NSString * pageLabelText = [NSString stringWithFormat:@"%d/%d",(int)index+1,(int)_photos.count];
NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:pageLabelText];
[str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:range];
_currentPageLabel.attributedText = str;
range:將要設(shè)置特殊字體的位置。
NSFontAttributeName:設(shè)置字體。
NSForegroundColorAttributeName:設(shè)置字體顏色。
//設(shè)置字體顏色
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
NSUnderlineStyleAttributeName:設(shè)置下劃線。
......
還有很多的屬性,需要的親們可以自己在xcode中查看。