From 943845a8efffa5c417ad0d28c493e83bbb825b87 Mon Sep 17 00:00:00 2001 From: stephenriolo Date: Wed, 19 Oct 2016 12:44:22 +0200 Subject: [PATCH] - Added handling of aliases in style.json --- StyleKit/Stylist.swift | 40 +++++++++++++++++++--- StyleKitDemo/StyleKitDemo/Style/style.json | 14 +++++--- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/StyleKit/Stylist.swift b/StyleKit/Stylist.swift index b3559a0..aa6b331 100644 --- a/StyleKit/Stylist.swift +++ b/StyleKit/Stylist.swift @@ -6,13 +6,27 @@ class Stylist { typealias setValueForControlState = @convention(c) (AnyObject, Selector, AnyObject, UInt) -> Void let data: Style + let aliases: Style let styleParser: StyleParsable var currentComponent: AnyClass? var viewStack = [UIAppearanceContainer.Type]() init(data: Style, styleParser: StyleParsable?) { self.styleParser = styleParser ?? StyleParser() - self.data = data + + var tmpAlias: Style = [:] + var tmpData: Style = [:] + + for (key, value) in data { + if key.hasPrefix("@") { + tmpAlias[key] = value + } else { + tmpData[key] = value + } + } + + self.data = tmpData + self.aliases = tmpAlias } func apply() { @@ -78,14 +92,33 @@ class Stylist { if let styles = object as? Stylist.Style { var stylesToApply = Stylist.Style() for (style, value) in styles { - stylesToApply[style] = styleParser.getStyle(forName: name, value: value) + stylesToApply[style] = styleParser.getStyle(forName: name, value: self.getValue(value)) } callAppearanceSelector(selectorName, valueOne: stylesToApply as AnyObject?, valueTwo: state) } else { - let value = styleParser.getStyle(forName: name, value: object) + let value = styleParser.getStyle(forName: name, value: self.getValue(object)) callAppearanceSelector(selectorName, valueOne: value, valueTwo: state) } + } + + /** + Checks if the value is an alias, and if it is resolves to the correct value. + Otherwise, it will return the passed value. + + - Parameter value: The alias name or actual value + */ + private func getValue(_ value: AnyObject) -> AnyObject { + guard value is String, value.hasPrefix("@") else { + return value + } + if let aliasValue = aliases[value as! String] { + return aliasValue + } else { + SKLogger.error("Value declared is using Alias \(value) which has not been defined") + + return value + } } private func callAppearanceSelector(_ selector: String, valueOne: AnyObject?, valueTwo: AnyObject?) { @@ -136,5 +169,4 @@ class Stylist { } } } - } diff --git a/StyleKitDemo/StyleKitDemo/Style/style.json b/StyleKitDemo/StyleKitDemo/Style/style.json index e78d1bb..6f0292c 100644 --- a/StyleKitDemo/StyleKitDemo/Style/style.json +++ b/StyleKitDemo/StyleKitDemo/Style/style.json @@ -1,9 +1,13 @@ { + "@mainFont": "HelveticaNeue-Bold:20.0", + "@primaryColor": "#000000", + "@secondaryColor": "#000FFF", + "@transperancyValue": 90, "StyleKitDemo.SKNavigationBar": { - "tintColor": "#000", + "tintColor": "@secondaryColor", "titleTextAttributes": { "NSColor": "#99000000", - "NSFont": "HelveticaNeue-Light:18.0" + "NSFont": "@mainFont" } }, "UITableView": { @@ -21,12 +25,12 @@ } }, "StyleKitDemo.SKButton": { - "backgroundColor:normal": "#cd0073", + "backgroundColor:normal": "@primaryColor", "backgroundColor:highlighted": "#8000c6", "titleColor:normal": "#fff", "titleColor:highlighted": "#fff", "font": "HelveticaNeue-Light:13.0", - "transparency":70 + "transparency":"@transperancyValue" }, "UIButton": { "font": "HelveticaNeue-Medium:13.0", @@ -83,4 +87,4 @@ "UIActivityIndicatorView": { "color": "#cd0073" } -} \ No newline at end of file +}