如何在swift中以编程方式为UIlabel提供动态高度?
问题描述:
我已经使用for循环生成的UIlabel,每个类型的diff文本都在标签中分配,我想根据文本动态地给出UIlabel大小
I have taken UIlabel which are generated dynamically using for loop , each type diff text is assign in label , I want to give UIlabel size dynamically depending on text
是在swift中有什么简单的解决办法吗?
Is there any easy solution in to do that in swift?
答
let label:UILabel = UILabel(frame: CGRectMake(x, y, width, height))
label.numberOfLines = 4
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
let font = UIFont(name: "Helvetica", size: 20.0)
label.font = font
label.text = "Whatever you want the text enter here"
label.sizeToFit()
如果你想根据文字内容设置numberOfLines,请给出你的最大行数。这里非常重要。
If you want to set numberOfLines according to the content of text,give your maximum lines.That is very important here.