//創(chuàng)建Label
UILabel* label = [[UILabelalloc]initWithFrame:CGRectMake(20,100,280,60)];
//設(shè)置label的背景顏色
label.backgroundColor= [UIColorredColor];
//添加label到視圖上
[self.windowaddSubview:label];
//設(shè)置文字
label.text=@"hello, world! welcome to!hello, world! welcome to!";
//設(shè)置文字居中
label.textAlignment=NSTextAlignmentCenter;
//設(shè)置文字顏色
label.textColor= [UIColorwhiteColor];
//設(shè)置文字大小
label.font= [UIFontsystemFontOfSize:22];
//設(shè)置文字粗體
label.font= [UIFontboldSystemFontOfSize:22];
//設(shè)置文字斜體(不支持中文斜體)
label.font= [UIFontitalicSystemFontOfSize:22];
//設(shè)置文字行數(shù),設(shè)置為0行就是自動(dòng)計(jì)算行數(shù),并換行
label.numberOfLines=0;
//得到系統(tǒng)字體簇
NSArray* arr = [UIFontfamilyNames];
for(NSString* nameinarr) {
NSLog(@"%@", name);
}
//根據(jù)frame適應(yīng)字體梗夸,改變字體大小
label.adjustsFontSizeToFitWidth=YES;
//改變UILabel的字體顏色以及大小
self.testLabel.text! =self.str
letr =NSMutableAttributedString.init(string:self.str)
r.addAttribute(NSForegroundColorAttributeName, value:UIColor.redColor(), range:NSMakeRange(0,2));
r.addAttribute(NSForegroundColorAttributeName, value:UIColor.grayColor(), range:NSMakeRange(3,5));
r.addAttribute(NSFontAttributeName, value:UIFont.systemFontOfSize(30.0), range:NSMakeRange(0,2));
self.testLabel.attributedText= r