先用两个宏定义
#define JKWidth [UIScreen mainScreen].bounds.size.width #define JKHeight [UIScreen mainScreen].bounds.size.height
1.创建UILabel(给label一个白色的底色)
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 64, JKWidth-40, 60)]; label.backgroundColor = [UIColor whiteColor]; [self.view addSubview:label];
2.给label添加字体
label.text = @"创建label";
3.给label设置剧中方式(三种常用方式:左中右),下面以剧中为例
// 左边 label.textAlignment = NSTextAlignmentLeft; // 居中 label.textAlignment = NSTextAlignmentCenter; // 右边 label.textAlignment = NSTextAlignmentRight;
4.label的字体大小设置
label.font = [UIFont systemFontOfSize:25.f];
5.label的字体颜色的设置
label.textColor = [UIColor redColor];
6.label的透明度
label.alpha = 0.5;
7.label是否隐藏(上面的label)
label.hidden = YES;
8.label的换行
label.numberOfLines = 0;
9.label的高度height计算
label.height = [self string:label.text sizeWithFont:[UIFont systemFontOfSize:15.f] maxSize:CGSizeMake(JKWidth-40, 1000)].height; -(CGSize)string:(NSString *)string sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize{ NSDictionary *attrs = @{NSFontAttributeName:font}; return [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attrs context:nil].size; }
10.在可见的宽高范围内字体的大小变化适应label的宽高范围
label.adjustsFontSizeToFitWidth = YES;
11.系统字体的加粗
label.font = [UIFont boldSystemFontOfSize:16.0f];