以编程方式多行在 iOS 中不起作用

Programatically Multiline not working in iOS

我想在 ios.Xamarin 中以编程方式设置多行 UILabel。

我以这种方式创建了 UILabel 并将其添加到我的视图中,下面是代码。

代码:

public override void ViewDidLoad()
        {
            base.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.AddSubview(label);
            secondContainer.AddConstraint(NSLayoutConstraint.Create(label, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, null, NSLayoutAttribute.NoAttribute, 0, 20));

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

但我的标签不超过一行。

注:

this 2 property not working well

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

我已经尝试在 google 和 SO 上搜索,但没有任何帮助。

我们将不胜感激。

确保 UILabel 宽度不够 space,因此需要 "grow"。

也可以在你的 UILabel 中使用 SizeToFit() 方法,例如:

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

希望对您有所帮助!