iOStyleLibrary 2.0.1

iOStyleLibrary 2.0.1

Yuriy Savitskiy 维护。



  • 作者

iOS Style Library

元素样式管理库

示例

要运行示例项目,请先克隆仓库,然后在 Example 目录中运行 pod install

要求

iOS 11

安装

iOStyleLibrary 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行

pod 'iOStyleLibrary'

iOStyleLibrary 版本

以下表格显示了您应该为项目使用 iOStyleLibrary 的哪个版本。

iOStyleLibrary 描述
2.0 ThemeStyle
1.0 UIViewStyle

用法

为想要应用样式的类添加扩展

例如

extension UIView: ITheme {
    /*
    It is extending the UIView class to support object styles
    
    If you want to apply for all objects of objective C, use
    extension NSObject: ITheme {
    }
    */
}

样式

添加样式

struct Style
{
    static let borderView = ThemeStyle<UIView> { view in
        view.layer.cornerRadius = 15
        view.layer.borderWidth = 2
        view.backgroundColor = .orange
    }
}
    

或者向Stylist结构添加动作

import iOStyleLibrary

public extension Stylist where SourceType: UIView {
    @discardableResult
    
    @discardableResult
    func alpha(_ alpha: CGFloat) -> Stylist<SourceType> {
        source.alpha = alpha
        return self
    }
}

使用它

Style.borderView.apply(to: self.subrectView)

//OR 

self.rectView.stylist.apply(Style.borderView)

self.rectView.stylist.alpha(0.5)

连接

self.label.stylist
            .fontSize(22)
            .text("newText")
            .textColor(.systemRed)

let newLabelStyle = Style.boldLabel + Style.purpleLabel + Style.italicLabel
self.label.stylist.apply(newLabelStyle)

状态

private enum AnyEnumStyleState {
	case exampleState1
        case exampleState2
}

private func prepareStyleStates() {
        
        self.rectView.stylist.prepareState(state: AnyEnumStyleState.exampleState1,
                                           style: Style.alpha(0.5) + Style.justView )
                        
        self.rectView.stylist.prepareState(state: AnyEnumStyleState.exampleState2,
                                           action: {  view in
                                                UIView.animate(withDuration: 1) {
                                                    view.alpha = 1
                                                    view.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
                                                    view.backgroundColor = .systemYellow
                                                }
                                            })
        
        self.subrectView.stylist.prepareState(state: AnyEnumStyleState.exampleState1,
                                              style: Style.borderView)
        
        self.subrectView.stylist.prepareState(state: AnyEnumStyleState.exampleState2,
                                              style: Style.justView)
}

//and use it 
	//switch to state 
        self.rectView.stylist.state = AnyEnumStyleState.exampleState1        
	//switch to another state      
        self.rectView.stylist.state = AnyEnumStyleState.exampleState2

这些示例的源文件在示例目录中。祝您玩得开心!

许可协议

MIT 许可协议。有关更多信息,请参阅License.md。版权所有 (c) 2020 Yuriy Savitskiy。