Sukhdeep Singh Kalra
Sukhdeep Singh Kalra

Reputation: 827

create UILabel with dynamic height according to size of label text

How to create a uilabel in swift with dynamic height so that line breaks automatically.

label = UILabel();
var str = " khduhfi8wde fwei9jfriuwqef iwfiuwqef iefiedf ejfiejfi9e      eihjfiefi8e eifiejh";
label.frame = CGRectMake(50, 50, 100, 100);
label.text = str;
view.addSubview(label);

Upvotes: 1

Views: 965

Answers (1)

Jean Le Moignan
Jean Le Moignan

Reputation: 22236

In Interface Builder, set a high priority to the width of the label, then set the hugging priority to high.

Also, do not forget to set numberOfLines to 0.

Text will then display with dynamic height.

Upvotes: 1

Related Questions