StyleKit一个iOS的CSS框架

网友投稿 549 2022-10-19

StyleKit一个iOS的CSS框架

StyleKit一个iOS的CSS框架

At a Glance

Front-end developers, who create layout for HTML pages, know the simplicity and power of CSS classes. At some point I thought: why not to make something similar for native iOS apps? This idea was quite obvious. When the first version of the framework has been done, it was named StyleKit. Obvious name for obvious thing.

How To Get Started

Copy content of Source folder to your project.

or

Use UIStyle cocoapod

Requirements

iOS 9.0 and laterXcode 8 and later

Usage

Style is a set of UI attributes. Each style includes at least one attribute, but can include unlimited collection of attributes.

/* * Create simple style with one attribute. */let attributes: [ViewStyleAttribute] = [ .backgroundColor(color: .yellow)]let yellowBackground = ViewStyle(attributes: attributes)/* * Another way to create the same style. */let anotherYellowBackground = ViewStyle.with(attribute: .backgroundColor(color: .yellow)) .done()/* * Create style with multiple attributes. */let greenBackgroundWithThinRedBorder = ViewStyle.with(attribute: .backgroundColor(color: .green)) .and(attribute: .borderColor(color: .red)) .and(attribute: .borderWidth(width: 1.0)) .done()

Full list of attributes is available in ViewStyleAttribute.swift file.

Any style can be applied to any view. You can apply unlimited number of styles to the same view.

/* * Apply style to view. */view.stl.apply(style: yellowBackground)/* * Apply multiple styles to view. */view.stl.apply(style: yellowBackground) .apply(style: greenBackgroundWithThinRedBorder)

Recommended way to manage styles in app is to implement a structure with static styles:

struct StyleStorage { static let defaultBackground = ViewStyle.with(attribute: .backgroundColor(color: .white)) .and(attribute: .borderColor(color: .green)) .and(attribute: .borderWidth(width: 2.0)) .done() static let thinOrangeText = ViewStyle.with(attribute: .textColor(color: .orange)) .and(attribute: .font(font: UIFont.systemFont(ofSize: 36.0, weight: UIFontWeightThin))) .done() }

Those styles can be reused many times in different places of the app:

override func viewDidLoad() { super.viewDidLoad() /* * Initialize view. */ view.stl.apply(style: StyleStorage.defaultBackground) /* * Initialize title label. */ titleLabel.stl.apply(style: StyleStorage.thinOrangeText)}

Also, it's possible to check programmatically if style supports view:

if StyleStorage.thinOrangeText.supports(view: helloLabel) { helloLabel.stl.apply(style: StyleStorage.thinOrangeText)}

License

StyleKit is available under the MIT license. See the LICENSE file for more info.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
下一篇:springboot获取properties属性值的多种方式总结
相关文章

 发表评论

暂时没有评论,来抢沙发吧~