国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS學習之UILable上顯示不同的字體和顏色

2019-11-14 18:31:58
字體:
來源:轉載
供稿:網友

在項目開發中,我們經常會遇到在這樣一種情形:在一個UILabel 使用不同的顏色或不同的字體來體現字符串,在iOS 6 以后我們可以很輕松的實現這一點,官方的API 為我們提供了UILabel類的attributedText, 使用不同顏色和不同字體的字符串,我們可以使用NSAttributedText 和 NSMutableAttributedText 類來實現。

現實代碼:

1
2
3
4
@interface ViewController : UIViewController
@PRoperty (nonatomic, strong) IBOutlet UILabel *attrLabel;
- (IBAction)next:(id)sender;
@end

 .m文件 在viewDidLoad方法中添加以下代碼:

1
2
3
4
5
6
7
8
9
self.title = @"For iOS 6 & later";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;

 

效果圖:

如果想在iOS6.0以前版本實現這個效果,需要使用到一個第三方庫TTTAttributedLabel,同時還有導入CoreText.frame框架.

原文轉自:http://my.oschina.net/CarlHuang/blog/138363


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 商南县| 古浪县| 巴林右旗| 石城县| 新民市| 靖宇县| 漳州市| 呼玛县| 山东省| 肃北| 和政县| 曲阜市| 富裕县| 新绛县| 西丰县| 福建省| 青冈县| 黔西县| 乐清市| 尼勒克县| 定西市| 和田市| 阳原县| 祥云县| 玛多县| 武清区| 四川省| 清徐县| 兰西县| 富裕县| 桃园县| 岑溪市| 普宁市| 资中县| 洛宁县| 车致| 淮安市| 固阳县| 东兰县| 宽城| 阿图什市|