Skip to content

Instantly share code, notes, and snippets.

@Erkened
Last active July 4, 2017 16:00
Show Gist options
  • Save Erkened/bbb38d40ee937bf6da19 to your computer and use it in GitHub Desktop.
Save Erkened/bbb38d40ee937bf6da19 to your computer and use it in GitHub Desktop.
Functions I reuse often in my apps. Swift 3.0
//
// Functions.swift
// Repnote
//
// Created by Jonathan Neumann on 23/03/2017.
// Copyright © 2017 Audioy. All rights reserved.
//
import Foundation
import UIKit
// getUIColorObjectFromHex function takes HEX and Alpha converts then returns a UIColor object. Used by UIColor extension
func getUIColorObjectFromHex(_ hex:String, alpha:CGFloat) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString = cString.substring(from: cString.characters.index(cString.startIndex, offsetBy: 1))
}
if (cString.characters.count != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(alpha)
)
}
// GENERATE A UUID
func createUUID() -> String{
let uuid = UUID().uuidString
return uuid
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment