Skip to content

Instantly share code, notes, and snippets.

@wtsnz
Last active October 28, 2015 10:35
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 wtsnz/a10e091a476dcbf4e818 to your computer and use it in GitHub Desktop.
Save wtsnz/a10e091a476dcbf4e818 to your computer and use it in GitHub Desktop.
Explaining the Theme.swift file
import Foundation
import UIKit
struct Theme {
/// Common padding for use in autolayout code
static let Padding = 20
/// Common app colors
struct Color {
static let White = UIColor(hex: 0xFFFFFF)
static let Green = UIColor(hex: 0x6EC13A)
static let Gray = UIColor(hex: 0x666666)
static let LightGray = UIColor(hex: 0x7F7F7F)
}
enum Font {
case Light
case Default
case Medium
case Bold
func fontWithSize(size: CGFloat) -> UIFont {
var font: UIFont?
switch (self) {
case .Light:
font = UIFont.systemFontOfSize(size, weight: UIFontWeightLight)
case .Default:
font = UIFont.systemFontOfSize(size, weight: UIFontWeightRegular)
case .Medium:
font = UIFont.systemFontOfSize(size, weight: UIFontWeightMedium)
case .Bold:
font = UIFont.systemFontOfSize(size, weight: UIFontWeightBold)
}
if let font = font {
return font
} else {
print("Font not found")
return UIFont.systemFontOfSize(size)
}
}
}
/// You can also define MotionEffects that you can add to UIViews in your app
/// simply by calling `self.titleLabel.addMotionEffect(Theme.MotionEffects.ForegroundMotionEffectGroup)`
/// Pretty neat!
struct MotionEffects {
/// Motion effect group to be added to all background elements to ensure consistency
static let BackgroundMotionEffectGroup: UIMotionEffectGroup = {
let value = -10
let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: .TiltAlongVerticalAxis)
verticalMotionEffect.maximumRelativeValue = value
verticalMotionEffect.minimumRelativeValue = -value
let horizontalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: .TiltAlongHorizontalAxis)
horizontalMotionEffect.maximumRelativeValue = value
horizontalMotionEffect.minimumRelativeValue = -value
let effectGroup = UIMotionEffectGroup()
effectGroup.motionEffects = [verticalMotionEffect, horizontalMotionEffect]
return effectGroup
}()
}
/// Configure all the UIAppearance Proxies.
static func applyAppearance() {
let titleTextAttributes = [
NSForegroundColorAttributeName: Theme.Color.White,
NSFontAttributeName: Theme.Font.Default.fontWithSize(17)
]
UINavigationBar.appearance().titleTextAttributes = titleTextAttributes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment