iOS基于UIKit实现的简易弹性盒子布局SNFlexView
2023-03-19 本文已影响0人
迷路的小小
SNFlexView是基于UIKit
和@resultBuilder
实现的简易的弹性盒子布局(仅支持自动布局)。
- 用法
resultBuilder
let flexView = SNFlexView(lineSpacing: 5, interitemSpacing: 5, contents: {
for str in contentStr {
if str.count > 5 {
UILabel(text: str)
.textColor(.white)
.padding(UIEdgeInsets(top: 2, left: 5, bottom: 2, right: 5))
.backgroundColor(.red)
.cornerRadius(5)
.masksToBounds(true)
}
else {
UILabel(text: str)
.textColor(.white)
.padding(UIEdgeInsets(top: 2, left: 5, bottom: 2, right: 5))
.backgroundColor(.blue)
.cornerRadius(5)
.masksToBounds(true)
}
}
})
-
view
数组
@SNViewBuilder
var contents: [UIView] {
for str in contentStr {
UILabel(text: str)
.textColor(.white)
.padding(UIEdgeInsets(top: 2, left: 5, bottom: 2, right: 5))
.backgroundColor(.red)
.cornerRadius(5)
.masksToBounds(true)
}
}
let flexView = SNFlexView(lineSpacing: 5, interitemSpacing: 5, contents: contents)
- 参数说明
- lineSpacing:行间距;
- interitemSpacing:列间距;
- contents:盒子内部子视图;
-
效果图
简易弹性盒子