ios – 如何设置UILabel只有宽度和高度和约束程序

前端之家收集整理的这篇文章主要介绍了ios – 如何设置UILabel只有宽度和高度和约束程序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用高度,宽度的方式创建一个UILabel,然后我想通过编程方式添加约束来定位UILabel.

更新:

我想像这样创建UI:

如何创建这个UI所有程序

创建一个标签label1的代码类似地,我创建了两个label2和label3

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)]];

我也可以设置垂直约束与视图,但我无法从一个标签到另一个标签设置约束.

解决方法

要创建高度和宽度约束的标签,这里是约束…不要忘了添加标签以查看与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]];

和斯威夫特

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,constant: 200))

检查这个link更多的细节

UPDATE
当你更新你的问题,这是我更新的答案…

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]];

结果屏幕

原文链接:https://www.f2er.com/iOS/336452.html

猜你在找的iOS相关文章