https://github.com/simplisticated/stylekit
CSS for iOS
https://github.com/simplisticated/stylekit
Last synced: 11 months ago
JSON representation
CSS for iOS
- Host: GitHub
- URL: https://github.com/simplisticated/stylekit
- Owner: simplisticated
- License: mit
- Created: 2016-11-23T12:28:23.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-29T01:11:48.000Z (over 9 years ago)
- Last Synced: 2025-05-08T17:04:01.178Z (11 months ago)
- Language: Swift
- Size: 66.4 KB
- Stars: 60
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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 later
* Xcode 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.
```swift
/*
* 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](https://github.com/igormatyushkin014/StyleKit/blob/master/Source/Engine/ViewStyleAttribute.swift) file.
Any style can be applied to any view. You can apply unlimited number of styles to the same view.
```swift
/*
* 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:
```swift
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:
```swift
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:
```swift
if StyleStorage.thinOrangeText.supports(view: helloLabel) {
helloLabel.stl.apply(style: StyleStorage.thinOrangeText)
}
```
# License
`StyleKit` is available under the MIT license. See the [LICENSE](./LICENSE) file for more info.