以UIView為例:
UIView *view1 = [UIView new];
view1.sd_layout都是點語法
距離左邊:view1.sd_layout.leftSpaceToView(self.view,20)
參數1是view1左邊以哪個視圖為基準,參數2是具體的數值距離上下右就把left換成對應的位置就行
對齊:leftEqualToView(self.view,20) ?需要其它對齊,將左變成需要的位置
寬高:1.具體數值:heightIs(100)widthIs(20)
2.根據其他視圖的比例來:heightRatioToView(self.view,0.5)
view1的高是view的0.5倍,就是一半
widthRatioToView(self.view,0.5)
view1的高是view的0.5倍,就是一
中心:centerYEqualToView(self.view)centerXEqualToView(self.view)
Label內容自適應:
① label的父視圖根據label的高度自適應
UILabel*subview1 = [UILabelnew];//初始化子label
subview1.text=@"這個label會根據這些文字內容高度自適應;而這個父view會根據label和view具體情況實現高度自適應。\nGot
it! OH YAEH!這個label會根據這些文字內容高度自適應;而這個父view會根據label和view具體情況實現高度自適應。\nGot
it! OH YAEH!";
UIView*subview2 = [UIViewnew];//初始化子view2
subview2.backgroundColor= [UIColororangeColor];
//將子view添加進父view
[self.view1sd_addSubviews:@[subview1,
subview2]];
subview1.sd_layout
.leftSpaceToView(self.view1,10)
.rightSpaceToView(self.view1,10)
.topSpaceToView(self.view1,10)
.autoHeightRatio(0);//設置文本內容自適應,如果這里的參數為大于0的數值則會以此數值作為view的高寬比設置view的高度
subview2.sd_layout
.topSpaceToView(subview1,10)
.widthRatioToView(subview1,1)
.heightIs(30)
.leftEqualToView(subview1);
//view1使用高度根據子view內容自適應,所以不需要設置高度,而是設置“[self.view1
setupAutoHeightWithBottomView:testView bottomMargin:10];”實現高度根據內容自適應
self.view1.sd_layout
.leftSpaceToView(self.view,10)
.topSpaceToView(self.view,80)
.rightSpaceToView(self.view,10);
//設置view1高度根據子其內容自適應
[self.view1setupAutoHeightWithBottomView:subview2bottomMargin:10];
②label寬度自適應
UILabel*autoWidthlabel = [UILabelnew];
autoWidthlabel.backgroundColor= [[UIColororangeColor]colorWithAlphaComponent:0.5];
_autoWidthLabel= autoWidthlabel;
autoWidthlabel.font= [UIFontsystemFontOfSize:12];
autoWidthlabel.text=@"寬度自適應(距離父view右邊距10)";
[self.viewaddSubview:autoWidthlabel];
autoWidthlabel.sd_layout
.rightSpaceToView(self.view,10)
.heightIs(20)
.bottomSpaceToView(self.view,50);
[autoWidthlabelsetSingleLineAutoResizeWithMaxWidth:180];
③label高度自適應
UILabel*autoHeightlabel = [UILabelnew];
autoHeightlabel.backgroundColor= [[UIColorredColor]colorWithAlphaComponent:0.5];
autoHeightlabel.font= [UIFontsystemFontOfSize:12];
autoHeightlabel.text=@"高度自適應(距離父view左邊距10,底部和其右側label相同,寬度為100)";
[self.viewaddSubview:autoHeightlabel];
autoHeightlabel.sd_layout
.bottomEqualToView(_autoWidthLabel)
.leftSpaceToView(self.view,10)
.widthIs(100)
.autoHeightRatio(0);
設置一排固定間距自動寬度子view
- (void)setupAutoWidthViewsWithCount:(NSInteger)count margin:(CGFloat)margin
{
_autoWidthViewsContainer= [UIViewnew]; //放button的父視圖
_autoWidthViewsContainer.backgroundColor= [UIColorgreenColor];
[self.viewaddSubview:_autoWidthViewsContainer];
NSMutableArray*temp = [NSMutableArraynew];
for(inti =0; i < count; i++) {
UIView*view = [UIViewnew];
view.backgroundColor= [UIColororangeColor];
[_autoWidthViewsContaineraddSubview:view];
view.sd_layout.autoHeightRatio(0.5);//設置高度約束
[tempaddObject:view];
}
_autoWidthViewsContainer.sd_layout
.leftSpaceToView(self.view,10)
.rightSpaceToView(self.view,10)
.topSpaceToView(_centerButton,10);
//此步設置之后_autoWidthViewsContainer的高度可以根據子view自適應
[_autoWidthViewsContainersetupAutoWidthFlowItems:[tempcopy]withPerRowItemsCount:countverticalMargin:marginhorizontalMargin:margin];
}
設置一排固定寬度自動間距子view
- (void)setupAutoMarginViewsWithCount:(NSInteger)count itemWidth:(CGFloat)itemWidth
{
_autoMarginViewsContainer= [UIViewnew];
_autoMarginViewsContainer.backgroundColor= [UIColorblueColor];
[self.viewaddSubview:_autoMarginViewsContainer];
NSMutableArray*temp = [NSMutableArraynew];
for(inti =0; i < count; i++) {
UIView*view = [UIViewnew];
view.backgroundColor= [UIColororangeColor];
[_autoMarginViewsContaineraddSubview:view];
view.sd_layout.autoHeightRatio(0.5);//設置高度約束
[tempaddObject:view];
}
_autoMarginViewsContainer.sd_layout
.leftSpaceToView(self.view,10)
.rightSpaceToView(self.view,10)
.topSpaceToView(_autoWidthViewsContainer,10);
//此步設置之后_autoMarginViewsContainer的高度可以根據子view自適應
[_autoMarginViewsContainersetupAutoMarginFlowItems:[tempcopy]withPerRowItemsCount:countitemWidth:itemWidthverticalMargin:10];
}
cell高度自適應
第一步:在自定義cell的.m文件里面,寫上這個
[selfsetupAutoHeightWithBottomView:_view4bottomMargin:10];
// view4是在cell所有的子視圖里面最下面的view
第二步:在返回cell高度的方法里面
intindex = indexPath.row%5;
NSString*str =_textArray[index];
// >>>>>>>>>>>>>>>>>>>>> * cell自適應步驟2 * >>>>>>>>>>>>>>>>>>>>>>>>
/* model為模型實例,keyPath為model的屬性名,通過kvc統一賦值接口*/ ?keypath:比如你要顯示的是str,str對應的model的屬性是text
return[self.tableViewcellHeightForIndexPath:indexPathmodel:strkeyPath:@"text"cellClass:[TestCell2class]contentViewWidth:[selfcellContentViewWith]];
attributedString
//? --------- attributedString測試:行間距為8 ---------------------------
NSString*text =@"attributedString";
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStylealloc]init];
[paragraphStylesetLineSpacing:8];
UIColor*color = [UIColorblackColor];
NSAttributedString*string = [[NSAttributedStringalloc]initWithString:textattributes:@{NSForegroundColorAttributeName: color,NSParagraphStyleAttributeName:
paragraphStyle}];
UILabel*label = [UILabelnew];
[self.viewaddSubview:label];
label.attributedText= string;
label.sd_layout
.leftSpaceToView(self.view,10)
.rightSpaceToView(self.view,10)
.topSpaceToView(self.view,70)
.autoHeightRatio(0);
//標注lable的text為attributedString
label.isAttributedContent=YES;
view的動態圓角處理以及UIScrollview的高度自適應,要想顯示的是圓,前提是view的寬高相等
UIScrollView*scrollView = [UIScrollViewnew];
[self.viewaddSubview:scrollView];
[scrollViewsd_addSubviews:@[self.view0,self.view1,self.view2,self.view3,self.view4,self.view5,self.view6,self.view7,self.view8]];
scrollView.sd_layout.spaceToSuperView(UIEdgeInsetsZero);
self.view0.sd_layout
.leftSpaceToView(scrollView,20)
.rightSpaceToView(scrollView,20)
.topSpaceToView(scrollView,20)
.heightIs(150);
self.view1.sd_layout
.widthIs(200)
.heightIs(200)
.centerXEqualToView(scrollView)
.topSpaceToView(self.view0,20);
self.view2.sd_layout
.leftSpaceToView(scrollView,50)
.rightSpaceToView(scrollView,50)
.topSpaceToView(self.view1,20)
.heightIs(150);
self.view3.sd_layout
.widthIs(250)
.heightEqualToWidth()
.centerXEqualToView(scrollView)
.topSpaceToView(self.view2,20);
// scrollview自動contentsize
[scrollViewsetupAutoContentSizeWithBottomView:self.view3bottomMargin:20];
//設置圓角
self.view0.sd_cornerRadiusFromHeightRatio=@(0.5);//設置view0的圓角半徑為自身高度的0.5倍
self.view1.sd_cornerRadiusFromWidthRatio=@(0.5);
self.view2.sd_cornerRadiusFromWidthRatio=@(0.5);