【问题标题】:Programatically Multiline not working in iOS以编程方式多行在 iOS 中不起作用
【发布时间】:2017-10-22 22:15:01
【问题描述】:

我想在 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;
        }

但我的标签不超过一行。

注意:

这 2 个属性不能正常工作

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

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

我们将不胜感激。

【问题讨论】:

    标签: ios xamarin xamarin.ios uilabel multiline


    【解决方案1】:

    确保 UILabel 宽度没有足够的空间,因此它需要“增长”。

    同时在你的 UILabel 中使用SizeToFit() 方法,类似于:

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-08-31
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      相关资源
      最近更新 更多