Skip to content

Instantly share code, notes, and snippets.

@jandamm
Last active March 21, 2016 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jandamm/f1d66409920e6fc0dcfa to your computer and use it in GitHub Desktop.
Save jandamm/f1d66409920e6fc0dcfa to your computer and use it in GitHub Desktop.
InVision Craft-Plugin Styles for Swift
//
// Example.swift
//
// Created by Jan Dammshäuser
// Copyright © 2016 Jan Dammshäuser. All rights reserved.
//
import UIKit
class ExampleVC: UIViewController {
@IBOutlet weak var exampleLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let (font, color) = FontStyles.H2.getStyle() //getting values
exampleLbl.applyFontStyle(.H1) //only setting values
}
}
//
// Extensions.swift
//
// Created by Jan Dammshäuser
// Copyright © 2016 Jan Dammshäuser. All rights reserved.
//
import Foundation
import UIKit
extension UILabel {
func applyFontStyle(fontStyle: FontStyles) {
let (font, color) = fontStyle.getStyle()
self.font = font
self.textColor = color
}
}
extension ColorPalette {
class func rgba(r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat) -> UIColor {
return UIColor(red: r/255, green: g/255, blue: b/255, alpha: a)
}
}
extension Fonts {
func name(weight weight: Weights) -> String {
return "\(self.rawValue)\(weight.rawValue)"
}
}
extension FontStyles {
func getStyle() -> (UIFont, UIColor) {
return (font(), color())
}
}
//
// Styles.swift
//
// Created by Jan Dammshäuser
// Copyright © 2016 Jan Dammshäuser. All rights reserved.
//
import Foundation
import UIKit
class ColorPalette: UIColor {
// add new colors (you can copy the values directly from your Styles in Sketch
class func headerColor() -> UIColor {
return rgba(50, 50, 50, 1)
}
class func textColor() -> UIColor {
return rgba(0, 0, 0, 1)
}
}
enum Fonts: String {
// append new Font-Families below
case GillSans
// append new Font Types below
enum Weights: String {
case Regular = ""
case Italic = "-Italic"
case Light = "-Light"
}
}
enum FontStyles {
// append text-styles below
case H1, H2, TextStyle
// define fonts below
func font() -> UIFont {
switch self {
case .H1:
return UIFont(name: Fonts.GillSans.name(weight: .Light), size: 20)!
case .H2:
return UIFont(name: Fonts.GillSans.name(weight: .Italic), size: 15)!
default:
return UIFont(name: Fonts.GillSans.name(weight: .Regular), size: 12)!
}
}
// define font colors below
func color() -> UIColor {
switch self {
case .H1, .H2:
return ColorPalette.headerColor()
default:
return ColorPalette.textColor()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment