Ironman Ironman - 2 years ago 87
iOS Question

Programatically Multiline not working in iOS

I want to set UILabel with multiline programatically in ios.Xamarin.

I have created UILabel this way and add it to my View below is the Code.

Code :

public override void ViewDidLoad()

var label = new UILabel();
label.Frame = new CoreGraphics.CGRect(10, 10, secondContainer.Frame.Size.Width - 20, 20);
label.Text = "First Label First Label First Label First Label First Label First Label";
label.Lines = 0;
label.LineBreakMode = UILineBreakMode.WordWrap;
secondContainer.AddConstraint(NSLayoutConstraint.Create(label, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, null, NSLayoutAttribute.NoAttribute, 0, 20));

secondContainerHeight.Constant = label.Frame.Size.Height + 20;

but my label is not taking more than one Line.

Note :

this 2 property not working well

label.Lines = 0;
label.LineBreakMode = UILineBreakMode.WordWrap;

I have tried search on google and SO but nothing is help me.

Any Help will be Appreciated.

Answer Source

Make sure the UILabel width doesn't have enough space so it needs to "grow".

Also use the SizeToFit() method in your UILabel, something like:

label.Lines = 0;
label.SizeToFit ();
label.LineBreakMode = UILineBreakMode.WordWrap;

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download