iOS label 自动换行 的相关问题

UILabel *textLabel = [[UILabel alloc] init];

textLabel.font = [UIFont systemFontOfSize:16];

NSString *str = @"222222222222222222222222222222222222222222";

textLabel.text = str;

textLabel.backgroundColor = [UIColor redColor];

textLabel.numberOfLines = 0;//根据最大行数需求来设置

textLabel.lineBreakMode = NSLineBreakByTruncatingTail;

CGSize maximumLabelSize = CGSizeMake(100, 9999);//labelsize的最大值

//关键语句

CGSize expectSize = [textLabel sizeThatFits:maximumLabelSize];

//别忘了把frame给回label,如果用xib加了约束的话可以只改一个约束的值

textLabel.frame = CGRectMake(20, 70, expectSize.width, expectSize.height);

[self.view addSubview:textLabel];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容