大佬教程收集整理的这篇文章主要介绍了ios – 如何设置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)]];
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更多的细节
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]];
结果屏幕
以上是大佬教程为你收集整理的ios – 如何设置UILabel只有宽度和高度和约束程序全部内容,希望文章能够帮你解决ios – 如何设置UILabel只有宽度和高度和约束程序所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。