SKStyleKit is an easy to use library for styling visual components. Written in swift it supports both swift and Objective C.
- Once declared style can be used across your app.
- Using StyleKit you can modify only style declaration you no longer have to edit xibs, storyboards or code.
- Styles are declared in easy to read and write JSON format.
To integrate SKStyleKit into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'SKStyleKit'
Clone the repo and drag files from Sources
folder into your Xcode project.
Before first call style kit should be initialized. It's recomended to do this in your app delegate:
import SKStyleKit
class AppDelegate: UIResponder, UIApplicationDelegate {
<...>
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
StyleKit.initStyleKit()
return true
}
<...>
}
Styles are declared in json format, add JSON file to your project, name it style.json
, and decalre first style:
{
"labelStyle": {
"fontSize": 15,
"fontColor": "#7F007F",
"borderWidth": 1,
"borderColor": "red",
"backgroundColor": "lightGray"
}
}
Now we have style named "labelStyle" with bunch of parameters, we already can use it, but let's decalre second one:
{
"titleLabelStyle": {
"fontSize": 25,
"fontColor": "#7F007F",
"borderWidth": 1,
"borderColor": "red",
"backgroundColor": "lightGray"
}
}
Ok, but both styles use same border settings and same fontColor, so decalration can be improved using inheritance:
{
"defBorder": {
"borderWidth": 1,
"borderColor": "red",
"backgroundColor": "lightGray"
},
"labelStyle": {
"parent": "defBorder",
"fontSize": 15,
"fontColor": "#7F007F"
},
"titleLabelStyle": {
"parent": "labelStyle",
"fontSize": 25
}
}
For complete list of parameters and full syntax description please check SKStyleKit parameters guide.
The most convenient (but not the only!) way to use styles is to use them with SK components or their subclasses. SK components already have properties style
and styleName
:
Select any label in xib or storyboard file and make it SKLabel class.
Then switch to attributes inspector and set style name property:
That's it!
Note: Styles also can be combined like "firstStyleName+secondStyleName+<...>"
SKButton has separate styles for differets states. Let's declare them:
{
"button.normal": {
"parents": ["button.Text", "button.ViewStyle"],
"alpha": 1
},
"button.selected": {
"backgroundColor": "green"
},
"button.highlighted": {
"alpha": 0.5
},
"button.disabled": {
"backgroundColor": "gray"
}
}
Note: Style for "normal" should set all parameters that going to be changed in other stats' styles
Then set them in Interface Bilder:
SKStyleKit entry point is StyleKit class.
To get instance of the certain style:
let style = StyleKit.style(withName: "StyleName")
Style can be applied to any standart controls like:
let style = StyleKit.style(withName: "StyleName")
style.apply(view: UIView?)
style.apply(slider: UISlider?)
style.apply(progress: UIProgressView?)
style.apply(label: UILabel?, text: String?)
style.apply(button: UIButton?, title: String?, forState state: UIControlState)
style.apply(switchControl: UISwitch?)
style.apply(textField: UITextField?, text: String?)
style.apply(textView: UITextView?, text: String?)
However in this case, you should not set text or attributedText property directly, use appropriate SKStyle method.
Styles can also be applied to strings/attributed strings:
{
"header3": {
"fontSize": 25,
"fontColor": "#7F007F"
}
}
let s = "Some string"
let style = StyleKit.style(withName: "header3")
SKStyle.string(withStyle: style, string: s) -> NSAttributedString?
SKStyle.string(withStyle: style, attributedString: NSAttributedString(string: s)) -> NSAttributedString?
These functions also allow to specify range to apply style like:
{
"header1": {
"fontSize": 25,
"fontColor": "#7F007F"
}
}
let s = "Some string"
let style = StyleKit.style(withName: "header1")
SKStyle.string(withStyle: style, string: s, range: NSRange(location: 0, length: 5)) -> NSAttributedString?
SKStyle.string(withStyle: style, attributedString: NSAttributedString(string: s), range: NSRange(location: 0, length: 8)) -> NSAttributedString?