Label初始化
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
//設(shè)置默認(rèn)文本
myLabel.text=@"這是一條數(shù)據(jù)";
//設(shè)置字體大小
myLabel.font = [UIFont systemFontOfSize:13];
//設(shè)置顏色
myLabel.textColor = [UIColor blueColor];
//添加到視圖上
[self.view addSubview:myLabel];
設(shè)置富文本(第2個(gè)位置開始,長度為1 設(shè)置顏色成紅色,17號(hào)字體)
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:myLabel.text];
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];
myLabel.attributedText = attributeString;
//當(dāng)然也可以通過字來獲取位置(獲取? 數(shù)據(jù)? 在字符串中的位置)
NSRange range = [myLabel.text rangeOfString:@"數(shù)據(jù)"];
設(shè)置對齊方式
/*
NSTextAlignmentLeft //左對齊
NSTextAlignmentCenter //居中
NSTextAlignmentRight? //右對齊
NSTextAlignmentJustified//最后一行自然對齊
NSTextAlignmentNatural //默認(rèn)對齊腳本
*/
myLabel.textAlignment = NSTextAlignmentCenter;//居中
設(shè)置對齊基線
/*
UIBaselineAdjustmentAlignBaselines //文本最上端與Label中線對齊,默認(rèn)值
UIBaselineAdjustmentAlignCenters? //文本中線與Label中線對齊
UIBaselineAdjustmentNone? ? ? ? ? //文本最下端與Label中線對齊
*/
//調(diào)整基線位置
myLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
//固定好frame 放不下自動(dòng)縮小字體
myLabel.adjustsFontSizeToFitWidth = YES;
設(shè)置文字裁剪方式
/*
NSLineBreakByWordWrapping = 0,//以空格為邊界,保留單詞
NSLineBreakByCharWrapping,? ? //保留整個(gè)字符
NSLineBreakByClipping,? ? ? ? //簡單剪裁,到邊界為止
NSLineBreakByTruncatingHead,? //按照"……文字"顯示
NSLineBreakByTruncatingTail,? //按照"文字……文字"顯示
NSLineBreakByTruncatingMiddle //按照"文字……"顯示
*/
myLabel.lineBreakMode = NSLineBreakByTruncatingHead;
//是否根據(jù)文本寬度改變字體大小
myLabel.adjustsFontSizeToFitWidth = YES;
設(shè)置最小字體
myLabel.minimumScaleFactor = 6;
設(shè)置行數(shù)
myLabel.numberOfLines = 2;
設(shè)置是否高亮
myLabel.highlighted = YES;//是否高亮
myLabel.highlightedTextColor = [UIColor redColor];//高亮顏色;
設(shè)置陰影
myLabel.shadowColor = [UIColor grayColor];//陰影顏色,默認(rèn)為nil
myLabel.shadowOffset = CGSizeMake(1, 1);//陰影的偏移點(diǎn)