自动布局:同一行上有两个标签,而一个标签是主标签,不应缩小

时间:2015-05-21 13:32:12

标签: ios autolayout uilabel

我有两个标签: title description 。他们应该站在一边

----------- --------------
| title   | |description |
----------- --------------

根据两个标签的内容," line"的最大宽度。应该使用,而一个标签是"主人"标签并始终显示。行为应如下:

Title Description
TitleTitleTitleTitleTitle Description
TitleTitleTitleTitleTitleTitle Descr…
TitleTitleTitleTitleTitleTitleTitleT…
Title DescriptionDescription
Title DescriptionDescriptionDescript…

有几种情况:

  1. 标题标签展开并将描述标签移到侧面。
  2. 标题标签扩展后,标题标签会缩写,描述标签应为"隐藏"。
  3. 当标题标签不包含太多文本时,描述标签可以展开。描述标签将展开,直到到达视图的末尾,然后缩写。
  4. 问题是约束应该如何?

    title 标签:

    • 左约束(父)
    • 居中Y约束(父母)
    • 宽度约束(< =父宽度,缩写标签)
    • 耐压缩性:750

    description 标签:

    • 左约束(标题标签右)
    • 中心Y约束(标题标签)
    • 右边约束(父)
    • 耐压缩性:250

    这就是我现在所拥有的。但它并没有像上面所描述的那样起作用。描述标签上的正确约束是有问题的,因为只有标签才会缩写。如果标题标签变短,使用它会将标签固定在右侧。我尝试了它的优先级,但其中的任何一个或。此外,未实现描述标签的隐藏(仅当我没有对描述标签具有正确的约束时,它将超过UIWindow,然后"消失")。

    我认为不可能隐藏"带有自动布局的描述标签。我只想计算宽度并手动隐藏它。

    图片和来源

    让我更清楚我想要的东西我已经制作了一些图片来证明这个问题。

    // 1: both short
    titleLabel.Text = "Lorem ipsum";
    descLabel.Text = "Anything";
    

    both short

    结果错误,因为描述标签太远了。它应该是

      

    Lorem ipsum dolor坐下来。任何

    // 2: title long, desc short
    titleLabel.Text = "Lorem ipsum dolor sit amet.";
    descLabel.Text = "Anything";
    

    title long, desc short

    相同。 1 因为标题标签太宽。

    // 3: title very long, desc short
    titleLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
    descLabel.Text = "Anything";
    

    title very long, desc short

    // 4: title short, desc long
    titleLabel.Text = "Something";
    descLabel.Text = "Lorem ipsum dolor sit amet.";
    

    title short, desc long

    // 5: title short, desc very long
    titleLabel.Text = "Something";
    descLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
    

    title short, desc very long

    // 6: both long
    titleLabel.Text = "Lorem ipsum dolor sit amet.";
    descLabel.Text = "Lorem ipsum dolor sit amet.";
    

    both long

    // 7: both very long
    titleLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
    descLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
    

    both very long

    // 8: title long, desc very long
    titleLabel.Text = "Lorem ipsum dolor sit amet.";
    descLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
    

    title long, desc very long

    // 9: title very long, desc long
    titleLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
    descLabel.Text = "Lorem ipsum dolor sit amet.";
    

    title very long, desc long

    我的代码似乎没有其他所有结果。最后但并非最不重要的是,我可以为您提供(C#)代码:

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
    
        // Perform any additional setup after loading the view, typically from a nib.
    
        container = new UIView();
        container.BackgroundColor = UIColor.Red;
        container.TranslatesAutoresizingMaskIntoConstraints = false;
        View.AddSubview(container);
    
        titleLabel = new UILabel();
        titleLabel.BackgroundColor = UIColor.Yellow;
        titleLabel.TranslatesAutoresizingMaskIntoConstraints = false;
        titleLabel.SetContentCompressionResistancePriority(750, UILayoutConstraintAxis.Horizontal);
        container.AddSubview(titleLabel);
    
        descLabel = new UILabel();
        descLabel.BackgroundColor = UIColor.Yellow;
        descLabel.TranslatesAutoresizingMaskIntoConstraints = false;
        descLabel.SetContentCompressionResistancePriority(250, UILayoutConstraintAxis.Horizontal);
        container.AddSubview(descLabel);
    
        NSMutableDictionary viewsDictionary = new NSMutableDictionary();
        viewsDictionary["container"] = container;
        viewsDictionary["parent"] = this.View;
        View.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[container]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
        View.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:|[container(==200)]", (NSLayoutFormatOptions)0, null, viewsDictionary));
    
        container.AddConstraint(NSLayoutConstraint.Create(titleLabel, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, container, NSLayoutAttribute.CenterY, 1, 0));
        container.AddConstraint(NSLayoutConstraint.Create(titleLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, container, NSLayoutAttribute.Left, 1, 10));
        // solves 3
        titleLabelWidth = NSLayoutConstraint.Create(titleLabel, NSLayoutAttribute.Width, NSLayoutRelation.LessThanOrEqual, container, NSLayoutAttribute.Width, 1, -20);
        container.AddConstraint(titleLabelWidth);
    
        container.AddConstraint(NSLayoutConstraint.Create(descLabel, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, container, NSLayoutAttribute.CenterY, 1, 0));
        container.AddConstraint(NSLayoutConstraint.Create(descLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, titleLabel, NSLayoutAttribute.Right, 1, 10));
        // solves the rest but breaks 1
        descWidth = NSLayoutConstraint.Create(descLabel, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, null, NSLayoutAttribute.NoAttribute, 1, 0);
        container.AddConstraint(descWidth);
        descRight = NSLayoutConstraint.Create(descLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, container, NSLayoutAttribute.Right, 1, -5);
        //container.AddConstraint (descRight);
    
        // test cases
        // 1: both short
        //          titleLabel.Text = "Lorem ipsum";
        //          descLabel.Text = "Anything";
        // 2: title long, desc short
        //          titleLabel.Text = "Lorem ipsum dolor sit amet.";
        //          descLabel.Text = "Anything";
        // 3: title very long, desc short
        //          titleLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
        //          descLabel.Text = "Anything";
        // result: X => solved (except desc label)
        // 4: title short, desc long
        //          titleLabel.Text = "Something";
        //          descLabel.Text = "Lorem ipsum dolor sit amet.";
        // 5: title short, desc very long
        //          titleLabel.Text = "Something";
        //          descLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
        // result: X
        // 6: both long
        //          titleLabel.Text = "Lorem ipsum dolor sit amet.";
        //          descLabel.Text = "Lorem ipsum dolor sit amet.";
        // 7: both very long
        //          titleLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
        //          descLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
        // 8: title long, desc very long
        //          titleLabel.Text = "Lorem ipsum dolor sit amet.";
        //          descLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
        // 9: title very long, desc long
        titleLabel.Text = "Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.";
        descLabel.Text = "Lorem ipsum dolor sit amet.";
    }
    
    public override void ViewWillLayoutSubviews()
    {
        base.ViewWillLayoutSubviews();
        if (titleLabelWidth.Constant + descWidth.Constant > container.Bounds.Size.Width)
        {
            //              if (descWidth.Constant > kDescMaxWidth){
            //                  descWidth.Constant = kDescMaxWidth;
            //              } else {
            container.RemoveConstraint(descRight);
            descLabel.Font = UIFont.SystemFontOfSize(0);
        //              }
        }
        else
        {
            container.AddConstraint(descRight);
        }
    }
    

    我怎么能解决不。 1/2或一般的问题?是否可以单独使用自动布局?

    解决方案:

    我完全采取了另一种方法(其他限制,......)。你可以找到我的方法here

1 个答案:

答案 0 :(得分:0)

您不能将描述标签固定到右侧,并为标题和描述添加两个宽度约束,然后选中。 要隐藏描述标签,您可以设置零字体

arm64