如何以编程方式仅设置 UILabel 的宽度和高度以及约束
How to set UILabel only width and height and constraints programmatically
我想以编程方式创建一个具有高度、宽度的 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]];
Swift 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]];
结果屏幕
在 Swift 3 中:(只需将 .Height 替换为 .height,将 .Equal 替换为 .equal)
self.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
我尝试使用此方法将高度更改为标签中的文本长度。
放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;
}
我想以编程方式创建一个具有高度、宽度的 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]];
Swift 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]];
结果屏幕
在 Swift 3 中:(只需将 .Height 替换为 .height,将 .Equal 替换为 .equal)
self.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
我尝试使用此方法将高度更改为标签中的文本长度。
放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;
}