SDAutoLayout(二)

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

Simulator Screen Shot 2017年7月14日 上午10.21.29.png

最后一句 設(shè)置為

[autoWidthlabel setSingleLineAutoResizeWithMaxWidth:KSCREEN_WIDTH];

Simulator Screen Shot 2017年7月14日 上午10.28.12.png

內(nèi)容自適應(yīng)

/*必須將[subView2 addSubview:imageView];先實(shí)現(xiàn) 才能再進(jìn)行布局,不然sd_layout 無法找到視圖進(jìn)行布局 */

- (void)setupAutoHeightView{
    
    UILabel *label = [UILabel new];
    label.text = @"嘻嘻嘻嘻哈哈哈哈哈哈哈哈還是打款發(fā)貨快睡覺打飛機(jī)卡死暴風(fēng)科技哈斯巴達(dá)克減肥吧圣誕節(jié)復(fù)活賽接電話福利局薩愛好就是大哥家哈桑的建安公司的會計(jì)   卡就是恐龍當(dāng)家卡機(jī)誰都會見客戶 \n空間撒謊都看見愛的空間啊 ";
    label.backgroundColor = [UIColor grayColor];
    
    UIView *subView2 = [UIView new];
    subView2.backgroundColor = [UIColor yellowColor];
    
//    將子View添加進(jìn)父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];先實(shí)現(xiàn) 才能再進(jìn)行布局,不然sd_layout 無法找到視圖進(jìn)行布局 */

    imageView.sd_layout
    .leftSpaceToView(subView2, 30)
    .topSpaceToView(subView2, 10)
    .widthIs(40)
    .heightIs(40);
    
    
}

內(nèi)容自適應(yīng)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容