如何以编程方式设置UILabel只有宽度和高度和约束

我想创build一个UILabel编程高度,宽度,然后我想添加约束,它也编程的定位UILabel。

更新:

我想要这样创build用户界面:

在这里输入图像说明

如何以编程方式创build这个UI

代码来创build一个标签label1类似地,我创build了两个标签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)]]; 

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

要创build具有高度和宽度约束的标签,这里是约束…并且不要忘记添加标签以使用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, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 200)) 

检查此链接的更多细节

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

结果屏幕

在这里输入图像说明

在Swift 3中:(只需用.heightreplace.Height,用.equalreplace成.Equal)

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

您可以设置标签的高度,宽度和位置

 yourLabe.frame = CGRectMake(X cordinate, y cordinate, Widht, Height); 
Interesting Posts