UILabel文本
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, self.view.bounds.size.width-40, 144)]; //初始化创建UILabel
[self.view addSubview:label];
label.backgroundColor = [UIColor whiteColor];//设置label背景颜色
label.textColor = [UIColor greenColor];//设置label文字颜色
label.numberOfLines = 0;//行数为0代表不限制行数,默认为1
label.font = [UIFont systemFontOfSize:16.f];//文字字体及大小,这里默认系统字体
label.textAlignment = NSTextAlignmentJustified;//文字对齐方式
label.lineBreakMode = NSLineBreakByTruncatingMiddle;//换行模式,可以根据字符,单词
label.highlighted = YES;//是否高亮
label.highlightedTextColor = [UIColor redColor];
label.shadowOffset = CGSizeMake(10, 10);//文字阴影大小
label.shadowColor = [UIColor blackColor];//阴影颜色
label.text = @"this is a label!this is a label!this this this is a label!this is a label!this is a label!this is a label!this this this is a label!this is a label!!this this this is a label!this is a label!this is a label!thi";//label的文字
CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16], } context:nil].size;//设置label大小可以随着内容的多少进行变化。
label.frame = CGRectMake(20, label.frame.origin.y, textSize.width, textSize.height);
CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16], } context:nil].size;
上面代码比较重要在动态计算label所占大小时,可以用到。