最近有個需求,要實(shí)現(xiàn)一個翻頁良狈,顯示當(dāng)前頁數(shù)和總頁數(shù)后添,頁數(shù)和總頁數(shù)字體大小不一樣。當(dāng)然了實(shí)現(xiàn)這個效果是很容易的薪丁,兩個label簡單搞定遇西。可是严嗜,我們使用一個label也是可以的喲粱檀。即將實(shí)現(xiàn)的效果圖如下:
Paste_Image.png
在這里我們可以使用label的一個屬性,叫做attributedText漫玄,很強(qiáng)大喲茄蚯。不說了上代碼:
//加1是為了讓page從1開始
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中查看皱碘。