Skip to content

Commit

Permalink
Add to git
Browse files Browse the repository at this point in the history
  • Loading branch information
MadGeorge committed Jan 10, 2017
1 parent c89d913 commit 9d8e44a
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 0 deletions.
55 changes: 55 additions & 0 deletions ColorHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// ColorHelpers.swift
// HRecipes
//
// Created by MadGeorge on 10/12/2016.
// Copyright © 2016 MadGeorge. All rights reserved.
//

import Foundation
import UIKit

extension UIColor {
/**
Create UIcolor from color hex representation

- Parameter hex: color hex string representation like #333333 or CCCCCC
- Returns color from hex

**Example:**
```
colorWithHexString("#333333")
colorWithHexString("333333")
colorWithHexString("CCCCCC")
colorWithHexString("5f9eb7")
```
*/
class func colorWithHexString (_ hex:String) -> UIColor {
var cString: String = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased();

if (cString.hasPrefix("#")) {
cString = (cString as NSString).substring(from: 1)
}

if (cString.characters.count != 6) {
return UIColor.gray
}

let rString = (cString as NSString).substring(to: 2)
let gString = ((cString as NSString).substring(from: 2) as NSString).substring(to: 2)
let bString = ((cString as NSString).substring(from: 4) as NSString).substring(to: 2)

var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
Scanner(string: rString).scanHexInt32(&r)
Scanner(string: gString).scanHexInt32(&g)
Scanner(string: bString).scanHexInt32(&b)

return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
}

/// Retutns default tint color #007AFF
class func defaultTintColor() -> UIColor {
// #007AFF
return UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
}
}
33 changes: 33 additions & 0 deletions GobalFunctions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// GobalFunctions.swift
// HRecipes
//
// Created by MadGeorge on 28/11/16.
// Copyright © 2016 MadGeorge. All rights reserved.
//

import Foundation

/// Shortcut for NSLocalizedString with empty comment
func L(_ key: String) -> String {
return NSLocalizedString(key, comment: "")
}

/// Run closure in background
func future(closure: @escaping ()->()) {
let backQueue = DispatchQueue.global()
backQueue.async(execute: closure)
}

/// Run closure after delay on main thread
func delayCall(delayInSeconds: Double, closure: @escaping()->()) {
let delay = DispatchTime.now() + delayInSeconds
DispatchQueue.main.asyncAfter(deadline: delay) {
closure()
}
}

/// Run any closure on main thread explisitly
func ui(closure: @escaping ()->()){
DispatchQueue.main.async(execute: closure)
}
69 changes: 69 additions & 0 deletions GradientView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import UIKit

/// Designable view with linear gradient settings
@IBDesignable
class GradientView: UIView {

@IBInspectable var topGradientColor: UIColor!
@IBInspectable var bottomGradientColor: UIColor!
@IBInspectable var locationTop: CGFloat = 0.0
@IBInspectable var locationBottom: CGFloat = 1.0

override func awakeFromNib() {
super.awakeFromNib()

backgroundColor = UIColor.clear
}

override func draw(_ rect: CGRect) {
let colorSpace = CGColorSpaceCreateDeviceRGB();
let context = UIGraphicsGetCurrentContext();

let gradientColors = [topGradientColor.cgColor, bottomGradientColor.cgColor];

let gradientLocations: [CGFloat] = [locationTop, locationBottom]

let gradient = CGGradient(colorsSpace: colorSpace, colors: gradientColors as CFArray, locations: gradientLocations)

let startPoint = CGPoint(x: 0, y: 0)

let endPoint = CGPoint(x: 0, y: bounds.height)

context?.drawLinearGradient(gradient!, start: startPoint, end: endPoint, options: [])
}

}

/// Designable view with radial gradient settings
@IBDesignable
class RadialGradientView: UIView {

@IBInspectable var topGradientColor: UIColor! = UIColor.black
@IBInspectable var bottomGradientColor: UIColor! = UIColor.white
@IBInspectable var startRadius: CGFloat = 10

override func awakeFromNib() {
super.awakeFromNib()

backgroundColor = UIColor.clear
}

override func draw(_ rect: CGRect) {
let colorSpace = CGColorSpaceCreateDeviceRGB();
let context = UIGraphicsGetCurrentContext();

let gradientColors = [topGradientColor.cgColor, bottomGradientColor.cgColor];

let gradientLocations: [CGFloat] = [0.0, 1.0]

let gradient = CGGradient(colorsSpace: colorSpace, colors: gradientColors as CFArray, locations: gradientLocations)

var radius = frame.width
if radius < frame.height {
radius = frame.height
}

context?.drawRadialGradient(gradient!, startCenter: center, startRadius: startRadius, endCenter: center, endRadius: radius, options: [])
}

}
94 changes: 94 additions & 0 deletions UIViewControllerHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// UIViewControllerHelpers.swift
// HRecipes
//
// Created by MadGeorge on 10/12/2016.
// Copyright © 2016 MadGeorge. All rights reserved.
//

import UIKit

extension UIViewController {

/// Present simple alert with title, message and cancel button
func alertSimple(_ title: String, message: String?, cancelTitle: String? = NSLocalizedString("alert_close_btn", comment: "")) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: L("alert_close_btn"), style: .cancel, handler: { a in}))
present(alert, animated: true, completion: nil)
}

func alertNotYet() {
alertSimple(L("Not implemented yet"), message: L("Implementation of this functionnality is in progress."))
}

}

/// View controller which hide it's title on disappear, so next controller back button title will be empty
class HideBarTitleVC: UIViewController {

var titleText: String? {
didSet {
navigationItem.title = titleText
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

navigationItem.title = ""
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

navigationItem.title = titleText
}

}

/// Table view controller which hide it's title on disappear, so next controller back button title will be empty
class HideBarTitleTVC: UITableViewController {

var titleText: String? {
didSet {
navigationItem.title = titleText
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

navigationItem.title = ""
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

navigationItem.title = titleText
}

}

/// Collection view controller which hide it's title on disappear, so next controller back button title will be empty
class HideBarTitleCVC: UICollectionViewController {

var titleText: String? {
didSet {
navigationItem.title = titleText
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

navigationItem.title = ""
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

navigationItem.title = titleText
}

}

0 comments on commit 9d8e44a

Please sign in to comment.