UILabel是從UIView:UIResponder:NSObject繼承來(lái)的
UILabel有個(gè)attributedtext屬性,是在IOS6.0以后出現(xiàn)的
1設(shè)置默認(rèn)文本
NSString *text = @"標(biāo)簽文本";
myLabel.text = text;
2設(shè)置標(biāo)簽文本(此屬性是iOS6.0之后才出現(xiàn),如若不是必要,不建議使用此屬性)
NSString *text = @"其實(shí)沒(méi)什么";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:text];
[attributeString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],? NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(2, 1)];
myLabel.attributedText = attributeString;
//創(chuàng)建uilabel
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
//設(shè)置背景色
label1.backgroundColor = [UIColor grayColor];
//設(shè)置tag
label1.tag = 91;
//設(shè)置標(biāo)簽文本
label1.text = @"Hello world!";
//設(shè)置標(biāo)簽文本字體和字體大小
label1.font = [UIFont fontWithName:@"Arial" size:30];
//設(shè)置文本對(duì)其方式
label1.textAlignment = UITextAlignmentCenter;
//文本對(duì)齊方式有以下三種
//typedef enum {
//? ? UITextAlignmentLeft = 0,左對(duì)齊
//? ? UITextAlignmentCenter,居中對(duì)齊
//? ? UITextAlignmentRight, 右對(duì)齊
//} UITextAlignment;
//文本顏色
label1.textColor = [UIColor blueColor];
//超出label邊界文字的截取方式
label1.lineBreakMode = UILineBreakModeTailTruncation;
//截取方式有以下6種
//typedef enum {
//? ? UILineBreakModeWordWrap = 0,? ? 以空格為邊界,保留整個(gè)單詞
//? ? UILineBreakModeCharacterWrap,? 保留整個(gè)字符
//? ? UILineBreakModeClip,? ? ? ? ? ? 到邊界為止
//? ? UILineBreakModeHeadTruncation,? 省略開(kāi)始,以……代替
//? ? UILineBreakModeTailTruncation,? 省略結(jié)尾,以……代替
//? ? UILineBreakModeMiddleTruncation,省略中間,以……代替,多行時(shí)作用于最后一行
//} UILineBreakMode;
//文本文字自適應(yīng)大小
label1.adjustsFontSizeToFitWidth = YES;
//當(dāng)adjustsFontSizeToFitWidth=YES時(shí)候,如果文本font要縮小時(shí)
//baselineAdjustment這個(gè)值控制文本的基線位置,只有文本行數(shù)為1是有效
label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
//有三種方式
//typedef enum {
//? ? UIBaselineAdjustmentAlignBaselines = 0, 默認(rèn)值文本最上端于label中線對(duì)齊
//? ? UIBaselineAdjustmentAlignCenters,//文本中線于label中線對(duì)齊
//? ? UIBaselineAdjustmentNone,//文本最低端與label中線對(duì)齊
//} UIBaselineAdjustment;
//文本最多行數(shù),為0時(shí)沒(méi)有最大行數(shù)限制
label1.numberOfLines = 2;
//最小字體,行數(shù)為1時(shí)有效,默認(rèn)為0.0
label1.minimumFontSize = 10.0;
//文本高亮
label1.highlighted = YES;
//文本是否可變
label1.enabled = YES;
//去掉label背景色
//label1.backgroundColor = [UIColor clearColor];
//文本陰影顏色
label1.shadowColor = [UIColor grayColor];
//陰影大小
label1.shadowOffset = CGSizeMake(1.0, 1.0);
//是否能與用戶交互
label1.userInteractionEnabled = YES;
[self.view addSubview:label1];
[label1 release];