UIDecoration 是一个帮助创建视图装饰的 Swift 框架
- iOS 12.0+
- Swift 5.0+
要使用 CocoaPods 将 SnapKit 集成到您的 Xcode 项目中,请在 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target '<Your Target Name>' do
pod 'UIDecoration'
end
只需将一个 DecorationItem 对象设置到装饰函数将设置样式,通常,应该使用 DecorationItem.r 来创建一个 DecorationItem 对象
let label = UILabel()
/// decoration —— font: regular12, textColor: black, textAlignment: center, numberOfLines: 0
label.decoration(.r.r12.color(.black).align(.center).unlimited)
let view = UIView.init()
/// decoration —— backgroundColor: orange, isUserInteractionEnabled: false
view.decoration(.r.ground(.orange).unInteraction)
一些装饰具有不同的特性,例如:text(_ value: String?) 将设置文本到 UILabel,将设置正常标题到 UIButton;font(_ value: UIFont?) 将设置文本到 UILabel,将设置 UIButton 的 UIButtonTitleLabel 字体;
let label = UILabel()
/// decoration —— text: "this is text", font: regular10
label.decoration(.r.text("this is text").font(.r10))
let button = UIButton.init(type: .custom)
/// decoration —— noralTitle: "this is text", font: regular10
button.decoration(.r.text("this is text").font(.r10))
每个装饰器只为有效的视图工作,例如:text(_ value: String?) 对 UILabel、UITextField、UITextView、UIButton 有效,但还可以将装饰设置到其他类型的视图,它只是无效并且不会崩溃
let label = UILabel()
/// decoration —— backgroundColor:red, text: "this is text", font: regular10
label.decoration(.r.ground(.red).text("this is text").font(.r10))
let view = UIView()
/// decoration —— backgroundColor:orange, text and font not support
view.decoration(.r.ground(.orange).text("this is text").font(.r10))
可以将相同的效果提取为独立的 DecorationItem 对象,并可以单独引用所需的视图,将根据顺序进行平等覆盖装饰
extension DecorationItem {
// decoration —— cornerRadius: 12, backgroundColor: white, shadow: black 16 4
var item: DecorationItem { radius(12).shadow(color: .black, blur: 16, offset: .init(width: 0, height: 4)) }
}
let view1 = UIView()
/// will use item decoration
view1.decoration(.r.item)
let view2 = UIView()
/// will use item decoration, and cover backgroundColor set to orange
view2.decoration(.r.item.ground(.orange))
也许您想扩展某些装饰到某些视图,例如,第三方视图 YYLabel 非常受欢迎,但它是 UIView 的子类而不是 UILabel,因此如果您想使用装饰设置字体、文本、颜色等,请使用协议 DecorationExtend
// extension Decoration to YYLabel
extension YYLabel: DecorationExtend {
public func paddingExtend(_ value: UIEdgeInsets) {
self.textContainerInset = value
}
public func linesExtend(_ value: Int) {
self.numberOfLines = UInt(value)
}
public func fontExtend(_ value: UIFont?) {
self.font = value
}
public func colorExtend(_ value: UIColor) {
self.textColor = value
}
public func attributedTextExtend(_ value: NSAttributedString?) {
self.attributedText = value
}
}
let label = YYLabel()
/// decoration —— text: "this is text", font: regular12, textColor: black
label.decoration(.r.text("this is text").r14.color(.black))
您可以使用 copyPush 函数创建自定义 DecorationItem,例如:创建 text2(_ value: String?)
// create custome decorationItem text2
extension DecorationItem {
@discardableResult func text2(_ value: String?) -> DecorationItem {
/// set copyPush key and function, is not set key, key will use #function
copyPush("text2") { view in
if let element = view as? UIButton {
element.setTitle(value, for: .normal)
}
if view is TextContainer {
view.setValue(value, forKey: "text")
}
}
}
}
let label = UILabel()
/// decoration —— text2: "this is decorateItem text2"
label.decoration(.r.text2("this is decorateItem text2 test"))