//初始化label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
//设置自动行数与字符换行
[label setNumberOfLines:0];
//设置线的风格
label.lineBreakMode = NSLineBreakByWordWrapping;
// 测试字串
label.text = @"这是一个测试!!!adsfsaf时发生发勿忘我勿忘我勿忘我勿忘我勿忘我阿阿阿阿阿阿阿阿阿阿阿阿阿啊00000000阿什顿。。。lvvvlvvjvjbjkjvzjhxvslkjvcjhgvhljhvbhbzjhvhzxvjgzvhjnhvckjkzhvjzvhzkvjzjchvzjjkvzjvjbdfsjjihvfkjzvcjkbzkhzvkhvsdhgyhvkjvjjvhjjzgjhvhgvhlszksvhzjkcvhfvhshvdhvbskjvjhjkvbdfvbhjsjvj hvhhfhgsdfuihvuhuhfvfdhvbifjvuhvjdihvkjuhh按计划V就好好发挥V计划符合及覅价格哈舒服就都是根据发发发好几个客户放假啊很关键啊都是;离开过寒假;飞机哥几个;和固话费;就带你给付款后恢复经济规划V就撒地方和规范GIF就更好更快速开发就爱个几哦啊好V精达股份hi按国家肯定撒符合国家韩国经适房和股份及快乐感觉撒放空间的身份和减法结构及节日快乐和关节UR经费投入根据房间很干净阿凡达刚好够看了会发觉大家机是覅及噶就是佛就回家感觉好久噶及阿萨符合国家撒房间爱久 ";
//设置背景颜色
label.backgroundColor = [UIColor grayColor];
//初始化段落 设置段落的风格
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
// 定义一个字典attributes 来设置lable的字体,和段落的风格
NSDictionary *attributes = @{NSFontAttributeName:label.font, NSParagraphStyleAttributeName:paragraphStyle.copy};
//计算frame的大小,并将lable的frame变成实际的大小
label.frame = [label.text boundingRectWithSize:CGSizeMake(400, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
//把lable显示在view上
[self.view addSubview: label];
截图:
}