GithubHelp home page GithubHelp logo

hhy5277 / stylekit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 146bc/stylekit

0.0 1.0 0.0 128 KB

A powerful & easy to use styling framework written in Swift

License: MIT License

Ruby 1.93% Swift 91.87% Objective-C 6.20%

stylekit's Introduction

alt text

Build Status Carthage compatible

StyleKit is a microframework that enables you to style your applications using a simple JSON file. Behind the scenes, StyleKit uses UIAppearance and some selector magic to apply the styles. You can also customize the parser for greater flexibility.

How does it work?

Create a JSON file in the following format

{
	"@headingFont": "HelveticaNeue-Bold:30.0",
	"UILabel": {
		"font": "@headingFont",
		"backgroundColor": "#000FFF"
	},
	"StyleKitDemo.SKView": {
		"StyleKitDemo.SKLabel": {
			"font": "HelveticaNeue-Bold:20.0",
			"backgroundColor": "#FFF000",
			"color": "#fff"
		},
		"StyleKitDemo.SKButton": {
			"font": "HelveticaNeue-Light:20.0",
			"titleColor:normal": "#FFFFFF",
			"titleColor:highlighted": "#000000"
		}
	},
	"StyleKitDemo.SKNavigationBar": {
		"titleTextAttributes": {
			"NSColor": "#000FFF",
			"NSFont": "@headingFont"
		}
	},
	"StyleKitDemo.SKTextField": {
		"font": "HelveticaNeue-Light:20.0",
		"textColor": "#000FFF"
	}
}

Load JSON file

AppDelegate.swift

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    if let styleFile = Bundle.main.url(forResource: "style", withExtension: "json") {
        StyleKit(fileUrl: styleFile)?.apply()
    }
    
    return true
}

On application launch the JSON file will be loaded and the styles applied.

The JSON file structure

Each object inside the JSON file should contain the name of the UIView as a key and the object inside should either contain the properties/functions that need to be set/called or another UIView, this will give you the ability to apply styles on views when contained in other views, an example of this would be the following.

{
	"UIButton": {
		"font": "HelveticaNeue-Bold:20.0"
	},
	"MyApp.LoginView": {
		"UIButton": {
			"font": "HelveticaNeue-Light:25.0"
		}
	}
}

This would apply HelveticaNeue-Bold with size 20 to all the UIButtons except the ones contained inside the LoginView class in your app.

Custom classes must be namespaced by the name of the module they are contained in. e.g. StyleKitDemo.SKTextField

Aliases

{
    "@mainFont": "HelveticaNeue-Bold:20.0",
    "@primaryColor": "#000FFF",
    "UIButton": {
        "font": "@mainFont"
    },
    "MyApp.LoginView": {
        "UIButton": {
            "font": "HelveticaNeue-Light:25.0",
            "titleColor:normal": "@primaryColor"
        }
    }
}

Bring Your Own Parser

StyleKit's initialiser supports passing a custom parser which should conform to the StyleParsable protocol.

Default Parser

class StyleParser: StyleParsable {

    func getStyle(forName name: String, value: AnyObject) -> AnyObject { 
        if let value = value as? String {
            if let font = FontHelper.parseFont(value) {
                return font
            } else if let color = ColorHelper.parseColor(value) {
                return color
            }
        }
        return value
    }
}

AppDelegate.swift

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    if let styleFile = Bundle.main.url(forResource: "style", withExtension: "json") {
        StyleKit(fileUrl: styleFile, styleParser: StyleParser())?.apply()
    }
    
    return true
}

Logging

By default, StyleKit will log any errors to the console. To customise the level of logging, you can pass a logLevel parameter as follows:

StyleKit(fileUrl: styleFile, logLevel: .debug)?.apply()

The levels of logging are:

  • .debug
  • .error (This is the default log level)
  • .severe
  • .none

How to install?

Carthage

Swift 3
github "146BC/StyleKit" ~> 0.6
Swift 4
github "146BC/StyleKit" ~> 0.7

CocoaPods

Add the 146BC Source

source 'https://github.com/146BC/Specs.git'
source 'https://github.com/CocoaPods/Specs.git'
Swift 3
pod 'StyleKit', '~> 0.6'
Swift 4
pod 'StyleKit', '~> 0.7'

stylekit's People

Contributors

albinekcom avatar jakubpetrik avatar jeanazzopardi avatar joekosowicz avatar phimage avatar stephenriolo 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.