【问题标题】:How to set UILabel only width and height and constraints programmatically如何以编程方式仅设置 UILabel 宽度和高度以及约束
【发布时间】:2015-10-04 09:34:22
【问题描述】:

我想以编程方式创建具有高度、宽度的 UILabel,然后我还想以编程方式为其添加约束以定位 UILabel。

更新:

我想创建这样的用户界面:

如何以编程方式创建此用户界面

创建一个标签label1 的代码类似地我创建了两个标签label2label3

UILabel *label1 = [[UILabel alloc]init];

label1.font = TitleFont;
label1.numberOfLines=0;
label1.text= @"Descriptions";
label1.lineBreakMode=NSLineBreakByWordWrapping;
[label1 sizeToFit];
label1.backgroundColor=[UIColor blueColor];
label1.textColor=[UIColor blackColor];
label1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:label1];

现在我可以使用此代码添加水平约束

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label1]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(label1)]];

我也可以使用视图设置垂直约束,但我无法设置从一个标签到另一个标签的约束。

【问题讨论】:

  • UILabel 可以使用sizeToFit
  • 如何使用sizeToFit 设置UILabel 的宽度和高度???

标签: ios objective-c uilabel nslayoutconstraint


【解决方案1】:

在这里创建具有高度和宽度约束的标签是约束...并且不要忘记使用addSubview 方法将标签添加到视图中

UILabel *Label = [[UILabel alloc] init];
[Label setTranslatesAutoresizingMaskIntoConstraints:NO];  

[self.view addSubview:Label];

// Width constraint
[Label addConstraint:[NSLayoutConstraint constraintWithItem:Label
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute: NSLayoutAttributeNotAnAttribute
                                                     multiplier:1
                                                       constant:200]];

// Height constraint
[Label addConstraint:[NSLayoutConstraint constraintWithItem:Label
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:nil
                                                      attribute: NSLayoutAttributeNotAnAttribute
                                                     multiplier:1
                                                       constant:21]];

斯威夫特 4:

label.translatesAutoresizingMaskIntoConstraints = false
label.addConstraint(NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 21))
label.addConstraint(NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200))

在 Swift 中

 Label.setTranslatesAutoresizingMaskIntoConstraints(false)
 self.view.addSubview(Label)

 Label.addConstraint(NSLayoutConstraint(item: Label, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 21))
 Label.addConstraint(NSLayoutConstraint(item: Label, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 200))  

查看link了解更多详情

更新
当你更新你的问题时,这是我更新的答案......

UILabel *Label1 = [[UILabel alloc] init];
[Label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
UILabel *Label2 = [[UILabel alloc] init];
[Label2 setTranslatesAutoresizingMaskIntoConstraints:NO];

Label1.text = @"Label1";
Label1.backgroundColor = [UIColor blueColor];
Label2.text = @"Label2";
Label2.backgroundColor = [UIColor redColor];

[self.view addSubview:Label1];
[self.view addSubview:Label2];

// Width constraint
[Label1 addConstraint:[NSLayoutConstraint constraintWithItem:Label1
                                                  attribute:NSLayoutAttributeWidth
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:nil
                                                  attribute: NSLayoutAttributeNotAnAttribute
                                                 multiplier:1
                                                   constant:280]];

// Height constraint
[Label1 addConstraint:[NSLayoutConstraint constraintWithItem:Label1
                                                  attribute:NSLayoutAttributeHeight
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:nil
                                                  attribute: NSLayoutAttributeNotAnAttribute
                                                 multiplier:1
                                                   constant:21]];

// CenterX constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view
                                                   attribute:NSLayoutAttributeCenterX
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:Label1
                                                   attribute: NSLayoutAttributeCenterX
                                                  multiplier:1
                                                    constant:0]];
// Top constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:Label1
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.topLayoutGuide
                                                      attribute: NSLayoutAttributeBottom
                                                     multiplier:1
                                                       constant:40]];


// label2
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:Label1
                                                      attribute:NSLayoutAttributeLeading
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:Label2
                                                      attribute: NSLayoutAttributeLeading
                                                     multiplier:1
                                                       constant:0]];
// label2.Height = label1.Height
[self.view  addConstraint:[NSLayoutConstraint constraintWithItem:Label1
                                                      attribute:NSLayoutAttributeHeight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:Label2
                                                      attribute: NSLayoutAttributeHeight
                                                     multiplier:1
                                                       constant:0]];
// label2.width = label1.width
[self.view  addConstraint:[NSLayoutConstraint constraintWithItem:Label1
                                                   attribute:NSLayoutAttributeWidth
                                                   relatedBy:NSLayoutRelationEqual
                                                      toItem:Label2
                                                   attribute: NSLayoutAttributeWidth
                                                  multiplier:1
                                                    constant:0]];

// label2.Top
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:Label2
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:Label1
                                                      attribute: NSLayoutAttributeBottom
                                                     multiplier:1
                                                       constant:34]];  

结果屏幕

【讨论】:

  • 你能给我一个例子来创建两个标签,一个底部到另一个,顶部约束或底部约束彼此
  • 拜托这很紧急我必须以编程方式创建一个 UI 设计,我还想为它们添加约束,因为不想通过提供 x,y 坐标来修复它们在屏幕上 UI 应该看起来像一个标签在它的顶部和第二个底部,就像我必须创建更多的两个 UIlabel
  • 实际上我没有找到任何好的资源来学习所有关于以编程方式添加约束的知识,这就是我问你的原因,你的知识非常高,我只想知道是否有任何单一资源。通过您的示例,现在我可以完全以编程方式制作任何 UI,但现在我面临的只有一个问题是现在知道 UI 中缺少的约束或额外的约束,视觉上我们可以看到这一点,但是通过以编程方式添加约束我们怎么能知道这一点?
  • 你可以在这里查看简单指南....matthewmorey.com/…
  • 很抱歉有负面和非建设性的评论,但这是一堆非常糟糕的代码。我希望人们在复制和粘贴之前会考虑一下。 ...不,我自己不会提供更好的答案;)。我只是想指出这一点。
【解决方案2】:

我尝试使用此方法将高度更改为标签中的文本长度。

把viewdidload

[yourlabel setNumberOfLines:0];
    CGRect mainframe = _lbl_board.frame;
    mainframe.size.height =  [self getLabelHeight:yourlabel];
    yourlabel.frame = mainframe;

方法

- (CGFloat)getLabelHeight:(UILabel*)label
{
    CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
    CGSize size;

    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:label.font}
                                                  context:context].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

    return size.height;
}

【讨论】:

    【解决方案3】:

    在 Swift 3 中:(只需将 .Height 替换为 .height 并将 .Equal 替换为 .equal)

    self.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
    

    【讨论】:

    • 最好使用 NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35).active = true跨度>
    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2013-06-23
    • 2015-07-15
    相关资源
    最近更新 更多