1.宽度自适应label
// 2.宽度自适应label
- (void)setupAutoWidthLabel
{
UILabel *autoWidthlabel = [UILabel new];
autoWidthlabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];
autoWidthlabel.font = [UIFont systemFontOfSize:12];
autoWidthlabel.text = @"宽度自适应(距离父view右边距10)";
[self.view addSubview:autoWidthlabel];
autoWidthlabel.sd_layout
.rightSpaceToView(self.view, 10)
.heightIs(20)
.bottomSpaceToView(self.view, 50);
[autoWidthlabel setSingleLineAutoResizeWithMaxWidth:180];
}
最后一句 设置为
[autoWidthlabel setSingleLineAutoResizeWithMaxWidth:KSCREEN_WIDTH];
则
内容自适应
/*必须将[subView2 addSubview:imageView];先实现 才能再进行布局,不然sd_layout 无法找到视图进行布局 */
- (void)setupAutoHeightView{
UILabel *label = [UILabel new];
label.text = @"嘻嘻嘻嘻哈哈哈哈哈哈哈哈还是打款发货快睡觉打飞机卡死暴风科技哈斯巴达克减肥吧圣诞节复活赛接电话福利局萨爱好就是大哥家哈桑的建安公司的会计 卡就是恐龙当家卡机谁都会见客户 \n空间撒谎都看见爱的空间啊 ";
label.backgroundColor = [UIColor grayColor];
UIView *subView2 = [UIView new];
subView2.backgroundColor = [UIColor yellowColor];
// 将子View添加进父View
[self.view sd_addSubviews:@[label,subView2]];
label.sd_layout
.leftSpaceToView(self.view, 10)
.rightSpaceToView(self.view, 10)
.topSpaceToView(self.view, 64)
.autoHeightRatio(0);//设置文本自适应
subView2.sd_layout
.topSpaceToView(label, 10)//距离label的底部高度
.widthRatioToView(label, 1)//与label的宽为1:1
.heightIs(70)//自身高度
.leftEqualToView(label);//与label左侧一样齐
self.view.sd_layout
.leftSpaceToView(self.view, 10)
.topSpaceToView(self.view, 80)
.rightSpaceToView(self.view, 10);
[self.view setupAutoHeightWithBottomView:subView2 bottomMargin:10];
//
UIImageView *imageView = [UIImageView new];
imageView.backgroundColor = [UIColor greenColor];
[subView2 addSubview:imageView];
/*必须将[subView2 addSubview:imageView];先实现 才能再进行布局,不然sd_layout 无法找到视图进行布局 */
imageView.sd_layout
.leftSpaceToView(subView2, 30)
.topSpaceToView(subView2, 10)
.widthIs(40)
.heightIs(40);
}