Skip to content

Instantly share code, notes, and snippets.

@leeprobert
Last active November 5, 2015 15:00
Show Gist options
  • Save leeprobert/338f46251eac3d17f113 to your computer and use it in GitHub Desktop.
Save leeprobert/338f46251eac3d17f113 to your computer and use it in GitHub Desktop.
StyleManager port to Swift 2. The class loads the plist by getting the filename from the project info.plist. The plist allows core UIKit components to be styled very easily. The StyleManager class is a Singleton and just needs to be initialised in the app delegate to style the app.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>config</key>
<dict>
<key>useNavBarShadow</key>
<false/>
<key>useTextShadows</key>
<true/>
<key>useIconEmboss</key>
<false/>
</dict>
<key>colors</key>
<dict>
<key>RGBA_example</key>
<string>0.1,0.55,0.65,1</string>
<key>menuBackgroundColor</key>
<string>#2d2d2f</string>
<key>contentBackgroundColor</key>
<string>#f2f2f2</string>
<key>primaryBrandColor</key>
<string>#ffea53</string>
<key>secondaryBrandColor</key>
<string>#0b6db7</string>
<key>tertiaryBrandColor</key>
<string>#006f3b</string>
<key>pantone660</key>
<string>#0b6db7</string>
<key>pantone107u</key>
<string>#ffea53</string>
<key>pantone349</key>
<string>#006f3b</string>
<key>pantone484</key>
<string>#9f1d21</string>
<key>pantone2603</key>
<string>#722b8d</string>
<key>pantone7530u</key>
<string>#ad9f96</string>
<key>pantone7528u</key>
<string>#d4c9c0</string>
<key>iconDefaultFillColor</key>
<string>lightGrayColor</string>
<key>iconDefaultSelectedFillColor</key>
<string>whiteColor</string>
<key>menuButtonBackgroundTint</key>
<string>primaryBrandColor</string>
<key>menuIconFillColor</key>
<string>primaryBrandColor</string>
<key>menuIconSelectedFillColor</key>
<string>primaryBrandColor</string>
<key>menuTitleColor</key>
<string>whiteColor</string>
<key>menuTitleSelectedColor</key>
<string>primaryBrandColor</string>
<key>menuSubTitleColor</key>
<string>lightGrayColor</string>
<key>menuSubTitleSelectedColor</key>
<string>lightGrayColor</string>
<key>navigationBarBackgroundColor</key>
<string>blackColor</string>
<key>navigationBarTintColor</key>
<string>primaryBrandColor</string>
<key>navigationBarTitleColor</key>
<string>primaryBrandColor</string>
<key>barButtonItemTextColor</key>
<string>primaryBrandColor</string>
<key>barButtonItemTintColor</key>
<string>primaryBrandColor</string>
<key>barButtonItemSelectedTextColor</key>
<string>whiteColor</string>
<key>barButtonItemDisabledTextColor</key>
<string>lightGrayColor</string>
<key>buttonTintColor</key>
<string>darkGrayColor</string>
<key>dashboardBackgroundColor</key>
<string>clearColor</string>
<key>progressViewTrackTintColor</key>
<string>lightGrayColor</string>
<key>progressViewTintColor</key>
<string>primaryBrandColor</string>
<key>pageControlCurrentPageIndicatorTintColor</key>
<string>redColor</string>
<key>pageControlPageIndicatorTintColor</key>
<string>lightGrayColor</string>
<key>refreshControlTintColor</key>
<string>darkGrayColor</string>
<key>segmentedControlTintColor</key>
<string>darkGrayColor</string>
<key>segmentedControlTextColor</key>
<string>lightGrayColor</string>
<key>segmentedControlSelectedTextColor</key>
<string>primaryBrandColor</string>
<key>tabBarItemTextColor</key>
<string>lightGrayColor</string>
<key>tabBarItemSelectedTextColor</key>
<string>primaryBrandColor</string>
<key>tabBarTintColor</key>
<string>primaryBrandColor</string>
<key>tabBarBarTintColor</key>
<string>redColor</string>
<key>tabBarBackgroundColor</key>
<string>blackColor</string>
<key>sliderMinTrackTintColor</key>
<string>primaryBrandColor</string>
<key>sliderMaxTrackTintColor</key>
<string>darkGrayColor</string>
<key>sliderThumbTintColor</key>
<string>darkGrayColor</string>
<key>stepperTintColor</key>
<string>primaryBrandColor</string>
<key>switchTintColor</key>
<string>lightGrayColor</string>
<key>switchOnTintColor</key>
<string>primaryBrandColor</string>
<key>switchThumbTintColor</key>
<string>darkGrayColor</string>
<key>tableViewSectionIndexColor</key>
<string>darkGrayColor</string>
<key>tableViewSectionIndexBackgroundColor</key>
<string>whiteColor</string>
<key>tableViewSectionIndexTrackBackgroundColor</key>
<string>primaryBrandColor</string>
<key>tableViewHeaderFooterColor</key>
<string>lightGrayColor</string>
<key>toolbarTintColor</key>
<string>lightGrayColor</string>
<key>toolbarBartintColor</key>
<string>greenColor</string>
<key>toolBarBackgroundColor</key>
<string>pinkColor</string>
<key>AliceBlue</key>
<string>#F0F8FF</string>
<key>AntiqueWhite</key>
<string>#FAEBD7</string>
<key>Aqua</key>
<string>#00FFFF</string>
<key>Aquamarine</key>
<string>#7FFFD4</string>
<key>Azure</key>
<string>#F0FFFF</string>
<key>Bisque</key>
<string>#FFE4C4</string>
<key>Black</key>
<string>#000000</string>
<key>BlanchedAlmond</key>
<string>#FFEBCD</string>
<key>Blue</key>
<string>#0000FF</string>
<key>BlueViolet</key>
<string>#8A2BE2</string>
<key>Brown</key>
<string>#A52A2A</string>
<key>BurlyWood</key>
<string>#DEB887</string>
<key>CadetBlue</key>
<string>#5F9EA0</string>
<key>Chartreuse</key>
<string>#7FFF00</string>
<key>Chocolate</key>
<string>#D2691E</string>
<key>Coral</key>
<string>#FF7F50</string>
<key>CornflowerBlue</key>
<string>#6495ED</string>
<key>Cornsilk</key>
<string>#FFF8DC</string>
<key>Crimson</key>
<string>#DC143C</string>
<key>Cyan</key>
<string>#00FFFF</string>
<key>DarkBlue</key>
<string>#00008B</string>
<key>DarkCyan</key>
<string>#008B8B</string>
<key>DarkGoldenRod</key>
<string>#B8860B</string>
<key>DarkGray</key>
<string>#A9A9A9</string>
<key>DarkGreen</key>
<string>#006400</string>
<key>DarkKhaki</key>
<string>#BDB76B</string>
<key>DarkMagenta</key>
<string>#8B008B</string>
<key>DarkOliveGreen</key>
<string>#556B2F</string>
<key>DarkOrange</key>
<string>#FF8C00</string>
<key>DarkOrchid</key>
<string>#9932CC</string>
<key>DarkRed</key>
<string>#8B0000</string>
<key>DarkSalmon</key>
<string>#E9967A</string>
<key>DarkSeaGreen</key>
<string>#8FBC8F</string>
<key>DarkSlateBlue</key>
<string>#483D8B</string>
<key>DarkSlateGray</key>
<string>#2F4F4F</string>
<key>DarkTurquoise</key>
<string>#00CED1</string>
<key>DarkViolet</key>
<string>#9400D3</string>
<key>DeepPink</key>
<string>#FF1493</string>
<key>DeepSkyBlue</key>
<string>#00BFFF</string>
<key>DimGray</key>
<string>#696969</string>
<key>DodgerBlue</key>
<string>#1E90FF</string>
<key>FireBrick</key>
<string>#B22222</string>
<key>FloralWhite</key>
<string>#FFFAF0</string>
<key>ForestGreen</key>
<string>#228B22</string>
<key>Fuchsia</key>
<string>#FF00FF</string>
<key>Gainsboro</key>
<string>#DCDCDC</string>
<key>GhostWhite</key>
<string>#F8F8FF</string>
<key>Gold</key>
<string>#FFD700</string>
<key>GoldenRod</key>
<string>#DAA520</string>
<key>Gray</key>
<string>#808080</string>
<key>Green</key>
<string>#008000</string>
<key>GreenYellow</key>
<string>#ADFF2F</string>
<key>HoneyDew</key>
<string>#F0FFF0</string>
<key>HotPink</key>
<string>#FF69B4</string>
<key>IndianRed</key>
<string>#CD5C5C</string>
<key>Indigo</key>
<string>#4B0082</string>
<key>Ivory</key>
<string>#FFFFF0</string>
<key>Khaki</key>
<string>#F0E68C</string>
<key>Lavender</key>
<string>#E6E6FA</string>
<key>LavenderBlush</key>
<string>#FFF0F5</string>
<key>LawnGreen</key>
<string>#7CFC00</string>
<key>LemonChiffon</key>
<string>#FFFACD</string>
<key>LightBlue</key>
<string>#ADD8E6</string>
<key>LightCoral</key>
<string>#F08080</string>
<key>LightCyan</key>
<string>#E0FFFF</string>
<key>LightGoldenRodYellow</key>
<string>#FAFAD2</string>
<key>LightGray</key>
<string>#D3D3D3</string>
<key>LightGreen</key>
<string>#90EE90</string>
<key>LightPink</key>
<string>#FFB6C1</string>
<key>LightSalmon</key>
<string>#FFA07A</string>
<key>LightSeaGreen</key>
<string>#20B2AA</string>
<key>LightSkyBlue</key>
<string>#87CEFA</string>
<key>LightSlateGray</key>
<string>#778899</string>
<key>LightSteelBlue</key>
<string>#B0C4DE</string>
<key>LightYellow</key>
<string>#FFFFE0</string>
<key>Lime</key>
<string>#00FF00</string>
<key>LimeGreen</key>
<string>#32CD32</string>
<key>Linen</key>
<string>#FAF0E6</string>
<key>Magenta</key>
<string>#FF00FF</string>
<key>Maroon</key>
<string>#800000</string>
<key>MediumAquaMarine</key>
<string>#66CDAA</string>
<key>MediumBlue</key>
<string>#0000CD</string>
<key>MediumOrchid</key>
<string>#BA55D3</string>
<key>MediumPurple</key>
<string>#9370DB</string>
<key>MediumSeaGreen</key>
<string>#3CB371</string>
<key>MediumSlateBlue</key>
<string>#7B68EE</string>
<key>MediumSpringGreen</key>
<string>#00FA9A</string>
<key>MediumTurquoise</key>
<string>#48D1CC</string>
<key>MediumVioletRed</key>
<string>#C71585</string>
<key>MidnightBlue</key>
<string>#191970</string>
<key>MintCream</key>
<string>#F5FFFA</string>
<key>MistyRose</key>
<string>#FFE4E1</string>
<key>Moccasin</key>
<string>#FFE4B5</string>
<key>NavajoWhite</key>
<string>#FFDEAD</string>
<key>Navy</key>
<string>#000080</string>
<key>OldLace</key>
<string>#FDF5E6</string>
<key>Olive</key>
<string>#808000</string>
<key>OliveDrab</key>
<string>#6B8E23</string>
<key>Orange</key>
<string>#FFA500</string>
<key>OrangeRed</key>
<string>#FF4500</string>
<key>Orchid</key>
<string>#DA70D6</string>
<key>PaleGoldenRod</key>
<string>#EEE8AA</string>
<key>PaleGreen</key>
<string>#98FB98</string>
<key>PaleTurquoise</key>
<string>#AFEEEE</string>
<key>PaleVioletRed</key>
<string>#DB7093</string>
<key>PapayaWhip</key>
<string>#FFEFD5</string>
<key>PeachPuff</key>
<string>#FFEFD5</string>
<key>Peru</key>
<string>#CD853F</string>
<key>Pink</key>
<string>#FFC0CB</string>
<key>Plum</key>
<string>#DDA0DD</string>
<key>PowderBlue</key>
<string>#B0E0E6</string>
<key>Purple</key>
<string>#800080</string>
<key>Red</key>
<string>#FF0000</string>
<key>RosyBrown</key>
<string>#BC8F8F</string>
<key>RoyalBlue</key>
<string>#4169E1</string>
<key>SaddleBrown</key>
<string>#8B4513</string>
<key>Salmon</key>
<string>#FA8072</string>
<key>SandyBrown</key>
<string>#F4A460</string>
<key>SeaGreen</key>
<string>#2E8B57</string>
<key>SeaShell</key>
<string>#FFF5EE</string>
<key>Sienna</key>
<string>#A0522D</string>
<key>Silver</key>
<string>#C0C0C0</string>
<key>SkyBlue</key>
<string>#87CEEB</string>
<key>SlateBlue</key>
<string>#6A5ACD</string>
<key>SlateGray</key>
<string>#708090</string>
<key>Snow</key>
<string>#FFFAFA</string>
<key>SpringGreen</key>
<string>#00FF7F</string>
<key>SteelBlue</key>
<string>#4682B4</string>
<key>Tan</key>
<string>#D2B48C</string>
<key>Teal</key>
<string>#008080</string>
<key>Thistle</key>
<string>#D8BFD8</string>
<key>Tomato</key>
<string>#FF6347</string>
<key>Turquoise</key>
<string>#40E0D0</string>
<key>Violet</key>
<string>#EE82EE</string>
<key>Wheat</key>
<string>#F5DEB3</string>
<key>White</key>
<string>#FFFFFF</string>
<key>WhiteSmoke</key>
<string>#F5F5F5</string>
<key>Yellow</key>
<string>#FFFF00</string>
<key>YellowGreen</key>
<string>#9ACD32</string>
<key>blackColor</key>
<string>blackColor</string>
<key>darkGrayColor</key>
<string>darkGrayColor</string>
<key>lightGrayColor</key>
<string>lightGrayColor</string>
<key>redColor</key>
<string>redColor</string>
<key>greenColor</key>
<string>greenColor</string>
<key>blueColor</key>
<string>blueColor</string>
<key>cyanColor</key>
<string>cyanColor</string>
<key>yellowColor</key>
<string>yellowColor</string>
<key>magentaColor</key>
<string>magentaColor</string>
<key>orangeColor</key>
<string>orangeColor</string>
<key>purpleColor</key>
<string>purpleColor</string>
<key>brownColor</key>
<string>brownColor</string>
<key>clearColor</key>
<string>clearColor</string>
</dict>
<key>fonts</key>
<dict>
<key>default</key>
<string>AvenirNext-Regular</string>
<key>defaultBold</key>
<string>AvenirNext-Bold</string>
<key>mainNavBarFont</key>
<string>mainFont18</string>
<key>barButtonItemFont</key>
<string>mainFont14</string>
<key>mainFont18</key>
<string>default:18.0</string>
<key>mainFont16</key>
<string>default:16.0</string>
<key>mainFont14</key>
<string>default:14.0</string>
<key>mainFont12</key>
<string>default:12.0</string>
<key>mainFont10</key>
<string>default:10.0</string>
<key>mainFont8</key>
<string>default:8.0</string>
<key>mainBoldFont18</key>
<string>defaultBold:18.0</string>
<key>mainBoldFont16</key>
<string>defaultBold:16.0</string>
<key>mainBoldFont14</key>
<string>defaultBold:14.0</string>
<key>mainBoldFont12</key>
<string>defaultBold:12.0</string>
<key>mainBoldFont10</key>
<string>defaultBold:10.0</string>
<key>mainBoldFont8</key>
<string>defaultBold:8.0</string>
</dict>
<key>metrics</key>
<dict>
<key>navigationBarTitleShadowOffset</key>
<string>1,1</string>
<key>navigationBarTitleVerticalPositionAdjustment</key>
<integer>0</integer>
<key>barButtonItemTextShadowOffset</key>
<string>1,1</string>
<key>segmentedControlNormalTextShadowOffset</key>
<string>1,1</string>
<key>segmentedControSelectedTextShadowOffset</key>
<string>-1,-1</string>
<key>defaultShadowOffset</key>
<string>1,1</string>
<key>defaultSelectedShadowOffset</key>
<string>-1,-1</string>
</dict>
<key>images</key>
<dict>
<key>nilImage</key>
<string>nil</string>
<key>navBarShadowImage</key>
<string>nilImage</string>
<key>navBarBackgroundImage</key>
<string>whiteBox.png:0,0,0,0:stretch</string>
<key>texturedImage</key>
<string>dashtexture.png</string>
<key>dashboardBackgroundTexture</key>
<string>&lt;ignore&gt;</string>
<key>menuButtonBackground</key>
<string>nilImage</string>
<key>defaultButtonSkin</key>
<string>menuBtn.png:10,10,10,10:stretch</string>
<key>iconHomeImage</key>
<string>icon:home:44:44:white</string>
<key>viewGradImage</key>
<string>view:GradRect:22:22</string>
<key>barButtonItemBackground</key>
<string>menuButtonBackground</string>
</dict>
</dict>
</plist>
//
// StyleManager.swift
// HoboApp
//
// Created by WLD_MBP_20 on 04/11/2015.
// Copyright (c) 2015 probert. All rights reserved.
//
import UIKit
class StyleManager: NSObject {
static let sharedInstance = StyleManager()
private let filenameKey:String = "Application styles filename"
private let defaultFontNameKey:String = "Avenir"
private var filename:String?
private var styleData = [String : AnyObject]()
private var colorsData = [String: AnyObject]()
private var imagesData = [String: AnyObject]()
private var fontsData = [String: AnyObject]()
private var metricsData = [String: AnyObject]()
private var configData = [String: AnyObject]()
// MARK: PUBLIC ------------------------
func loadStyles() {
filename = NSBundle.mainBundle().infoDictionary?[filenameKey] as? String
if let path = NSBundle.mainBundle().pathForResource(filename, ofType: "plist") {
if let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject> {
styleData = dict
colorsData = (styleData["colors"] as? [String: AnyObject])!
imagesData = (styleData["images"] as? [String: AnyObject])!
fontsData = (styleData["fonts"] as? [String: AnyObject])!
metricsData = (styleData["metrics"] as? [String: AnyObject])!
configData = (styleData["config"] as? [String: AnyObject])!
setAppearances()
}else{
print("Problem loading Styles")
}
}
}
// MARK: PRIVATE ------------------------
private func setAppearances(){
/*
UIBarButtonItem -----------------------------------------
*/
// Normal
UIBarButtonItem.appearance().setTitleTextAttributes(getTitleTextAttributesForComponent("UIBarButtonItem", state: UIControlState.Normal), forState: UIControlState.Normal)
UIBarButtonItem.appearance().tintColor = getColourForStyleKey("barButtonItemTintColor")
/*
UIButton -----------------------------------------------
*/
UIButton.appearance().tintColor = getColourForStyleKey("buttonTintColor")
/*
UINavigationBar -----------------------------------------
*/
UINavigationBar.appearance().titleTextAttributes = getTitleTextAttributesForComponent("UINavigationBar", state: UIControlState.Normal)
UINavigationBar.appearance().barStyle = UIBarStyle.BlackTranslucent
UINavigationBar.appearance().barTintColor = getColourForStyleKey("navigationBarBackgroundColor")
UINavigationBar.appearance().tintColor = getColourForStyleKey("navigationBarTintColor")
/*
UITabBar -------------------------------------------------
*/
UITabBar.appearance().backgroundColor = getColourForStyleKey("tabBarBackgroundColor")
UITabBar.appearance().barTintColor = getColourForStyleKey("tabBarBarTintColor")
UITabBar.appearance().tintColor = getColourForStyleKey("tabBarTintColor")
/*
UITabBarItem -----------------------------------------
*/
UITabBarItem.appearance().setTitleTextAttributes(getTitleTextAttributesForComponent("UITabBarItem", state: UIControlState.Normal), forState:UIControlState.Normal)
UITabBarItem.appearance().setTitleTextAttributes(getTitleTextAttributesForComponent("UITabBarItem", state: UIControlState.Selected), forState:UIControlState.Selected)
/*
UIToolbar -----------------------------------------
*/
UIToolbar.appearance().backgroundColor = getColourForStyleKey("toolbarBackgroundColor")
UIToolbar.appearance().barTintColor = getColourForStyleKey("toolbarBarTintColor")
UIToolbar.appearance().tintColor = getColourForStyleKey("toolbarTintColor")
/*
UIPageControl -----------------------------------------
*/
UIPageControl.appearance().tintColor = getColourForStyleKey("pageControlCurrentPageIndicatorTintColor")
/*
UIProgressView -----------------------------------------
*/
UIProgressView.appearance().progressTintColor = getColourForStyleKey("progressViewTintColor")
UIProgressView.appearance().trackTintColor = getColourForStyleKey("progressViewTrackTintColor")
/*
UIRefreshControl -----------------------------------------
*/
UIRefreshControl.appearance().tintColor = getColourForStyleKey("refreshControlTintColor")
/*
UISearchBar -----------------------------------------
*/
// UISearchBar.appearance().tintColor = getColourForStyleKey("searchBarTintColor")
/*
UISegmentedControl -----------------------------------------
*/
UISegmentedControl.appearance().setTitleTextAttributes(getTitleTextAttributesForComponent("UISegmentedControl", state: UIControlState.Normal), forState:UIControlState.Normal)
UISegmentedControl.appearance().setTitleTextAttributes(getTitleTextAttributesForComponent("UISegmentedControl", state: UIControlState.Selected), forState:UIControlState.Selected)
/*
UISlider -----------------------------------------
*/
UISlider.appearance().minimumTrackTintColor = getColourForStyleKey("sliderMinTrackTintColor")
UISlider.appearance().maximumTrackTintColor = getColourForStyleKey("sliderMaxTrackTintColor")
UISlider.appearance().thumbTintColor = getColourForStyleKey("sliderThumbTintColor")
/*
UIStepper -----------------------------------------
*/
UIStepper.appearance().tintColor = getColourForStyleKey("stepperTintColor")
/*
UISwitch -----------------------------------------
*/
UISwitch.appearance().tintColor = getColourForStyleKey("switchTintColor")
UISwitch.appearance().onTintColor = getColourForStyleKey("switchOnTintColor")
UISwitch.appearance().thumbTintColor = getColourForStyleKey("switchThumbTintColor")
/*
UITableView -----------------------------------------
*/
UITableView.appearance().sectionIndexColor = getColourForStyleKey("tableViewSectionIndexColor")
UITableView.appearance().sectionIndexBackgroundColor = getColourForStyleKey("tableViewSectionIndexBackgroundColor")
/*
UITableViewHeaderFooterView -----------------------------------------
*/
UITableViewHeaderFooterView.appearance().tintColor = getColourForStyleKey("tableViewHeaderFooterColor")
}
// ----------------------------------------------
private func getTitleTextAttributesForComponent(component: String, state: UIControlState) -> [String: AnyObject] {
var attrib = [String: AnyObject]()
if "UIBarButtonItem" == component {
if state == UIControlState.Normal {
attrib = [
NSForegroundColorAttributeName: getColourForStyleKey("barButtonItemTextColor"),
NSFontAttributeName: getFontForStyleKey("mainFont10")
]
}
}else if "UINavigationBar" == component {
if state == UIControlState.Normal {
attrib = [
NSForegroundColorAttributeName: getColourForStyleKey("navigationBarTitleColor"),
NSFontAttributeName: getFontForStyleKey("mainFont16")
]
}
}else if "UITabBarItem" == component {
if state == UIControlState.Normal {
attrib = [
NSForegroundColorAttributeName: getColourForStyleKey("barButtonItemTextColor"),
NSFontAttributeName: getFontForStyleKey("mainFont16")
]
}
}else if "UISegmentedControl" == component {
if state == UIControlState.Normal {
attrib = [
NSForegroundColorAttributeName: getColourForStyleKey("barButtonItemTextColor"),
NSFontAttributeName: getFontForStyleKey("mainFont16")
]
}
}
return attrib
}
// MARK: PUBLIC -------------------------------------------------
func getFontForStyleKey(key: String) -> UIFont {
var value:String? = fontsData[key] as? String;
/*
resolved?
*/
if nil == value {
// don't give up yet! Does the key need to be capitalised?
value = fontsData[key.capitalizedString] as? String
// still nil? Just return Arial 18pt font
if nil == value {
return UIFont(name: "ArialMT", size: 18.0)!
}
}
/*
We have a value but does the value resolve to another key?
*/
if nil != fontsData[value!] {
value = fontsData[value!] as? String
}
let properties:Array? = value!.componentsSeparatedByString(":")
if properties != nil && properties?.count == 2 {
var fontName:String? = properties![0]
let fontSize:String? = properties![1]
if fontName == "default" {
fontName = fontsData["default"] as? String
}
if fontName == "defaultBold" {
fontName = fontsData["defaultBold"] as? String
}
let font:UIFont? = UIFont(name: fontName!, size: CGFloat(NSString(string: fontSize!).floatValue))
return font ?? UIFont(name: "ArialMT", size: 18.0)!
}
// default Arial 18pt
return UIFont(name: "ArialMT", size: 18.0)!
}
//-------------------------------
func getColourForStyleKey(key: String) -> UIColor {
var value:String? = colorsData[key] as? String;
/*
resolved?
*/
if nil == value {
// don't give up yet! Does the key need to be capitalised?
value = colorsData[key.capitalizedString] as? String
// still nil? Just return black
if nil == value {
return UIColor.blackColor()
}
}
/*
We have a value but does the value resolve to another key?
*/
if nil != colorsData[value!] {
value = colorsData[value!] as? String
}
/*
Does the value refer to a UIColor static method?
*/
let selector:Selector = Selector(value!)
let isColorMethod:Bool = (selector != nil) ? UIColor.respondsToSelector(selector) : false
if isColorMethod {
// TODO: need to work out how to do this in Swift. performSelector for static methods doesn't seem to be supported.
let col = UIColor.performSelector(selector).takeUnretainedValue()
return col as! UIColor
}
/*
value could be HEX
*/
let isHex:Bool = value!.hasPrefix("#")
if isHex {
let hexcol:UIColor = UIColor(hexString: value!)
return hexcol
}
/*
value could be RGB
*/
let arr:Array = value!.componentsSeparatedByString(",")
let isRGB:Bool = arr.count == 4
if isRGB {
let rgbCol:UIColor = UIColor(rgbString: value!) // the extension requires the full string
return rgbCol
}
/*
Finally, it is possible to use an image as a texture fill
*/
let components:Array = value!.componentsSeparatedByString(":")
if components.count > 1 {
if components[0] == "tile" {
let tiledColor:UIColor = UIColor(patternImage: self.getImageForStyleKey(components[1]))
return tiledColor;
}
}
/*
If we get this far then the colour has not been convertible so we just return black
*/
return UIColor.blackColor()
}
//-------------------------------
func getImageForStyleKey(key:String) -> UIImage {
/*
Images are set in the plist like so :
image1.png:2,2,2,2:stretch
image2.jpg:4,2,4,2:tile
This is the image name, the edge insets and tile type
*/
var value:String? = imagesData[key] as? String;
/*
resolved?
*/
if nil == value {
return UIImage() // empty
}
/*
We have a value but does the value resolve to another key?
*/
if nil != imagesData[value!] {
value = imagesData[value!] as? String
}
if value!.lowercaseString == "nil" {
/*
Return empty image if you want to nullify a navBar shadow or background image
*/
return UIImage();
}
/*
If we have got this far the image value is either a resizable image set as a string like this "image1.png:2,2,2,2:stretch"
or it is an icon reference like this : "icon:iconName:44:44:white" in which case the icon is drawn at the set width and height and then returned as a UIImage
you would use this to create barButtonItem icons. The names of the icons are described in the IconSuper class and in the styling documentation.
Or it is a view class that you wish to be converted to an image at a certain size. The syntax in this case is view:ClassName:44:44
Resizable Image : "image1.png:2,2,2,2:stretch"
Icon : "icon:iconName:44:44:white" // NOT SUPPORTED YET
View : "view:ClassName:44:44" // NOT SUPPORTED YET
*/
let properties = value!.componentsSeparatedByString(":")
if properties.count == 1 {
return UIImage(named: value!) ?? UIImage()
}else{
let filename:String? = properties[0]
if nil == filename {
return UIImage()
}
let edgeInsetValues:Array = properties[1].componentsSeparatedByString(",")
let resizingModeType:String = properties[2].capitalizedString
let edgeInsets:UIEdgeInsets = UIEdgeInsetsMake( CGFloat(NSString(string: edgeInsetValues[0]).floatValue),
CGFloat(NSString(string: edgeInsetValues[1]).floatValue),
CGFloat(NSString(string: edgeInsetValues[2]).floatValue),
CGFloat(NSString(string: edgeInsetValues[3]).floatValue))
let resizingMode:UIImageResizingMode = resizingModeType == "Tile" ? UIImageResizingMode.Tile : UIImageResizingMode.Stretch
let imgA:UIImage? = UIImage(named:filename!)
if nil != imgA {
let imgB:UIImage? = imgA!.resizableImageWithCapInsets(edgeInsets, resizingMode: resizingMode)
return imgB ?? UIImage();
}else{
return UIImage()
}
}
}
//-------------------------------
func getMetricForKey(key: String) -> Float {
return metricsData[key] as? Float ?? 0.0
}
//-------------------------------
func getConfigForKey(key: String) -> Bool {
return metricsData[key] as? Bool ?? false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment