1.寬度自適應(yīng)label
// 2.寬度自適應(yīng)label
- (void)setupAutoWidthLabel
{
UILabel *autoWidthlabel = [UILabel new];
autoWidthlabel.backgroundColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];
autoWidthlabel.font = [UIFont systemFontOfSize:12];
autoWidthlabel.text = @"寬度自適應(yīng)(距離父view右邊距10)";
[self.view addSubview:autoWidthlabel];
autoWidthlabel.sd_layout
.rightSpaceToView(self.view, 10)
.heightIs(20)
.bottomSpaceToView(self.view, 50);
[autoWidthlabel setSingleLineAutoResizeWithMaxWidth:180];
}
最后一句 設(shè)置為
[autoWidthlabel setSingleLineAutoResizeWithMaxWidth:KSCREEN_WIDTH];
則
內(nèi)容自適應(yīng)
/*必須將[subView2 addSubview:imageView];先實現(xiàn) 才能再進行布局平匈,不然sd_layout 無法找到視圖進行布局 */
- (void)setupAutoHeightView{
UILabel *label = [UILabel new];
label.text = @"嘻嘻嘻嘻哈哈哈哈哈哈哈哈還是打款發(fā)貨快睡覺打飛機卡死暴風(fēng)科技哈斯巴達克減肥吧圣誕節(jié)復(fù)活賽接電話福利局薩愛好就是大哥家哈桑的建安公司的會計 卡就是恐龍當(dāng)家卡機誰都會見客戶 \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);//設(shè)置文本自適應(yīng)
subView2.sd_layout
.topSpaceToView(label, 10)//距離label的底部高度
.widthRatioToView(label, 1)//與label的寬為1:1
.heightIs(70)//自身高度
.leftEqualToView(label);//與label左側(cè)一樣齊
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];先實現(xiàn) 才能再進行布局轻抱,不然sd_layout 無法找到視圖進行布局 */
imageView.sd_layout
.leftSpaceToView(subView2, 30)
.topSpaceToView(subView2, 10)
.widthIs(40)
.heightIs(40);
}