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

标签 ios xamarin xamarin.ios uilabel multiline

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

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

代码:

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 宽度没有足够的空间,所以它需要“增长”。

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

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

希望这可以帮助!

关于ios - 以编程方式多行在 iOS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44126685/

相关文章:

android - 如何根据 API 响应触发应用内导航

ios - 如何将目标内容标识符附加到 Info.plist 中的快捷方式项?

ios - 从其他类访问按钮

c# - Xamarin:找不到绑定(bind)属性

c# - 当应用程序位于 xamarin.ios native 应用程序的前台时,如何获取推送/远程通知?

xamarin - 应用程序在 xamarin 中的 ios 设备中启动后终止

ios - IOS8 Xamarin 替换中带有 Datepicker 的 UIActionSheet

iphone - 如何在viewDidAppear方法或viewDidLoad中请求JSON数据?

iphone - 如何使用 MonoTouch 为 iPhone 更改带有自定义按钮的 UIKeyboard 的返回键?

objective-c - drawRect()中如何改变string的默认颜色,core graphic