Giter Club home page Giter Club logo

skstylekit's Introduction

SKStyleKit

Swift 4.2 compatible CocoaPods compatible Platform iOS License: MIT

SKStyleKit is an easy to use library for styling visual components. Written in swift it supports both swift and Objective C.

Features

  • 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.

Installation

CocoaPods

To integrate SKStyleKit into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'SKStyleKit'

Manual

Clone the repo and drag files from Sources folder into your Xcode project.

Getting Started

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 Declaration

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.

Basic Usage

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:

Label Example

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+<...>"

Button Example

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:

Advanced

Work With Styles Programmatically

SKStyleKit entry point is StyleKit class.

To get instance of the certain style:

let style = StyleKit.style(withName: "StyleName")

Using Style Kit With Non SK Components

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.

Using Style Kit With Attributed Strings

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?

skstylekit's People

Contributors

motylevm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.