如何以编程方式仅设置UILabel的宽度和高度以及约束

Var*_*ria 24 objective-c uilabel ios nslayoutconstraint

我想以编程方式创建一个UILabel高度,宽度,然后我想以编程方式为它添加约束以定位UILabel.

更新:

我想创建这样的UI:

在此输入图像描述

如何以编程方式创建此UI

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];
Run Code Online (Sandbox Code Playgroud)

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

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

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

EI *_*2.0 72

要创建具有高度和宽度约束的标签,这里是约束...并且不要忘记在使用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]];
Run Code Online (Sandbox Code Playgroud)

而在斯威夫特

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))
Run Code Online (Sandbox Code Playgroud)

查看此链接了解更多详情

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

 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))  
Run Code Online (Sandbox Code Playgroud)

结果屏幕

在此输入图像描述