SwiftyBeans
SwiftyBeans 是一个用于声明式 iOS 视图样式的 μ 库。编写可重用、可读和简洁的样式。
用法
let paragraphStyle: Style = .bean(
.textColor(.black),
.font("Avenir-Light"),
.fontSize(20),
.multiline
)
let accentTextStyle: Style = .bean(
.paragraphStyle,
.textColor(.blue)
)
label.style(accentTextStyle, .textAlign(.justified))
文档
所有使用的属性都放置在 Style 枚举中。
Beans
Beans 是样式模块的隐喻。Beans 简单地封装了任意数量的样式或另一个 beans,因为从技术上讲 beans 也是一种样式。
let module: Style = .bean(anotherBean, andAnotherBean, .backgroundColor(.red))
UIView 属性
背景颜色
.backgroundColor(UIColor)
背景图片
.backgroundImage(UIImage?)
内容模式
.contentMode(UIViewContentMode)
裁剪到边界
.clipsToBounds(Bool)
缩放
.scale(CGFloat)
// -- or
.scaleX(CGFloat)
// -- or
.scaleY(CGFloat)
平移
// First argument is axis, second y axis
.translate(CGFloat, CGFloat)
// -- or
.translateX(CGFloat)
// -- or
.translateY(CGFloat)
旋转
.rotate(CGFloat)
Alpha(不透明度)
.alpha(CGFloat)
可见性
.visible
.invisible
Z-index(z 轴提升)
.zIndex(CGFloat)
盒阴影
.shadow(color: UIColor = .black,
opacity: Float = 0.3,
verticalOffset: CGFloat = 0,
horizontalOffset: CGFloat = 0,
offset: CGSize? = nil,
radius: CGFloat = 8)
边框
.border(thick: CGFloat = 1.0, color: UIColor = .black)
单侧边框
.border(edges: UIRectEdge, thick: CGFloat = 1.0, color: UIColor = .black)
// -- Example usage
.border(edges: [.top, .bottom])
圆角半径
.cornerRadius(CGFloat)
单角圆角(注意:只适用于iOS 11.0及以上版本,如果失败则回退到所有四个角均设置圆角)
.cornerRadius(corner: Corner, _ radius: CGFloat)
线性渐变
.linearGradient(direction: GradientDirection = .toBottom,
locations: [NSNumber]? = nil,
_ colors: UIColor...)
// -- Example usage
.linearGradient(direction: .toRight, .blue, .white)
UIImageView 属性
图像
.image(UIImage?)
UILabel 属性
文字颜色
.textColor(UIColor)
UIFont
.font(UIFont)
// -- or
.fontName(String)
字体大小
.fontSize(CGFloat)
文字对齐
.textAlign(NSTextAlignment)
行数
// Sets UILabels numberOfLines to 0
.multiline
// -- or
.numberOfLines(Int)
安装
SwiftyBeans不包含任何外部依赖。
CocoaPods
# Podfile
pod 'SwiftyBeans'
然后在 Podfile
目录中
$ pod install
许可证
SwiftyBeans采用MIT许可证发布。请参阅LICENSE文件以获取详细信息。