ApplyStyleKit 0.3.1

ApplyStyleKit 0.3.1

shindyu 维护。



  • 作者
  • shindyu

ApplyStyleKit

Build Status Carthage compatible Cocoapods Compatible License Platform

ApplyStyleKit 是一个使用 Swifty Method Chain 对 UIKit 应用样式的库。

通常,应用样式到 UIView 等组件时,需要多次编写 propertyName 和等于操作符。

使用 ApplyStyleKit,您可以在代码中舒适地应用样式。

例如

    sampleLabel.applyStyle
        .backgroundColor(.yellow)
        .text("sample label")
        .textAlignment(.center)
        .textColor(.green)
        .font(.boldSystemFont(ofSize: 30.0))
        .numberOfLines(0)

安装

要求

  • iOS 9.0 或更高版本
  • Swift 4.2
  • Xcode 10

Carthage

  github "shindyu/ApplyStyleKit"

CocoaPods

target '<Your Target Name>' do
  pod 'ApplyStyleKit'
end

使用方法

import ApplyStyleKit

class ViewController: UIViewController {
    let sampleView = UIView()
    let sampleLabel = CustomLabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Apply style
        sampleView.applyStyle
            .backgroundColor(.red)
            .alpha(0.5)

        // When applying to layer
        sampleView.layer.applyStyle
            .cornerRadius(10)
            .borderColor(.gray)
            .borderWidth(2)

        // Of course, you can apply it if you inherit UIView etc.
        sampleLabel.applyStyle
            .text("Of course, you can apply it if you inherit UIView etc.")
            .textAlignment(.center)
            .textColor(.green)
            .font(.boldSystemFont(ofSize: 30.0))
            .numberOfLines(0)
    }
}

高级功能

你也可以创建自己的 applyStyleMethod。

为了能够定义扩展,base的访问修饰符是公有的,它是StyleObject的一个属性。

示例

extension StyleObject where Base: UIView {
    @discardableResult func specialStyle() -> StyleObject {
        base.backgroundColor = .red
        base.layer.cornerRadius = 10
        return self
    }
}

支持

QuartzCore

  • CALayer

UIKit

  • UIView
  • UILabel
  • UIButton
  • UIImageView
  • UISwitch
  • UIControl
  • UIStackView
  • UISlider
  • UITableView
  • UICollectionView

基本策略

ApplyStyleKit旨在针对无返回值的方法以及具有setter的属性。由于ApplyStyleKit的特性,我认为应该避免返回除StyleObject之外的类型。

贡献

如果有任何请求,请创建一个issue或pull request。

错误报告、文档或测试始终受欢迎!😂

许可证

ApplyStyleKit 以开源形式可用,遵循 MIT 许可协议