//创建UILabel
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];
//设置背景色
label1.backgroundColor = [UIColor grayColor];
//设置tag
label1.tag = 91;
//设置标签文本
label1.text = @CCBASE.NET!;
//设置标签文本字体和字体大小
label1.font = [UIFont fontWithName:@Arial size:30];
//设置文本对齐方式
label1.textAlignment = UITextAlignmentCenter;
//文本对齐方式有以下三种
typedef enum {
UITextAlignmentLeft = 0,左对齐
UITextAlignmentCenter,居中对齐
UITextAlignmentRight, 右对齐
} UITextAlignment;
//文本颜色
label1.textColor = [UIColor blueColor];
//超出label边界文字的截取方式
label1.lineBreakMode = UILineBreakModeTailTruncation;
//截取方式有以下6种
typedef enum {
UILineBreakModeWordWrap = 0, 以空格为边界,保留整个单词
UILineBreakModeCharacterWrap, 保留整个字符
UILineBreakModeClip, 到边界为止
UILineBreakModeHeadTruncation, 省略开始,以……代替
UILineBreakModeTailTruncation, 省略结尾,以……代替
UILineBreakModeMiddleTruncation,省略中间,以……代替,多行时作用于最后一行
} UILineBreakMode;
//文本文字自适应大小
label1.adjustsFontSizeToFitWidth = YES;
当adjustsFontSizeToFitWidth=YES时候,如果文本font要缩小时
baselineAdjustment这个值控制文本的基线位置,只有文本行数为1是有效
label1.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
//有三种方式
typedef enum {
UIBaselineAdjustmentAlignBaselines = 0, 默认值文本最上端于label中线对齐
UIBaselineAdjustmentAlignCenters,//文本中线于label中线对齐
UIBaselineAdjustmentNone,//文本最低端与label中线对齐
} UIBaselineAdjustment;
//文本最多行数,为0时没有最大行数限制
label1.numberOfLines = 2;
//最小字体,行数为1时有效,默认为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];
示例:UILabel根据字符串长度自动适应宽度和高度
//这个frame是初设的,没关系,后面还会重新设置其size。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
label.numberOfLines = 0;
label.backgroundColor = [UIColor clearColor];
NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20],};
NSString *str = @"abcdefg你上课可是你的拿到了";
CGSize textSize = [str boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
[label setFrame:CGRectMake(100, 100, textSize.width, textSize.height)];
label.textColor = [UIColor greenColor];
label.text = str;
[self.view addSubview:label];