Skip to content

Instantly share code, notes, and snippets.

@celian-m
Last active February 1, 2017 09:29
Show Gist options
  • Save celian-m/6de8f93ab310eaf872b289341a928ac4 to your computer and use it in GitHub Desktop.
Save celian-m/6de8f93ab310eaf872b289341a928ac4 to your computer and use it in GitHub Desktop.
Useful swift functions
//
// Style.swift
// Mouce
//
// Created by Célian MOUTAFIS on 22/07/2016.
// Copyright © 2016 mouce. All rights reserved.
// mouce.fr
import Foundation
import UIKit
func DLog(message: String, file: String = #file, function: String = #function, line: Int = #line, column: Int = #column) {
print("\(file.components(separatedBy: "/").last ?? file ) : \(function) : \(line) - \(message)")
}
extension UIViewController {
@IBAction func dismiss() {
self.dismiss(animated: true, completion: nil)
}
@IBAction func pop() {
let _ = self.navigationController?.popViewController(animated: true)
}
}
//MARK : UIColor
extension UIColor {
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
convenience init(netHex:Int) {
self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff)
}
}
class Color : UIColor {
class func color(_ name : String) -> UIColor {
var colorString = ~name
if name == "white" {
return white
}else if name == "blue" {
colorString = "#02205C"
}
//Add more cases
if !colorString.hasPrefix("#") {
return UIColor.yellow
}
colorString = colorString.substring(from: colorString.index(colorString.startIndex, offsetBy : 1))
let rString = colorString.substring(with: colorString.index(colorString.startIndex, offsetBy: 0)..<colorString.index(colorString.startIndex, offsetBy: 2))
let gString = colorString.substring(with: colorString.index(colorString.startIndex, offsetBy: 2)..<colorString.index(colorString.startIndex, offsetBy: 4))
let bString = colorString.substring(with: colorString.index(colorString.startIndex, offsetBy: 4)..<colorString.index(colorString.startIndex, offsetBy: 6))
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))
}
}
class ColoredViews : UIView {
@IBInspectable var mBackground : String = "" {
didSet {
let color = Color.color(name: mBackground)
self.backgroundColor = color
self.setNeedsDisplay()
}
}
}
//MARK: Fonts
protocol Font {
}
struct Material : Font {
static func regular(size: CGFloat) -> UIFont {
return UIFont(name: "MaterialIcons-Regular", size: size)!
}
}
struct Style {
private static func extract(_ string : String) -> (String, CGFloat)?{
let pattern = "(.*)_([0-9]+)$"
let regexp = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.init() )
let matches = regexp.matches(in: string, options: NSRegularExpression.MatchingOptions.init(), range: NSRange.init(location: 0, length: string.characters.count))
if let match = matches.first {
print(match)
let nameRange = match.rangeAt(1)
let sizeRange = match.rangeAt(2)
let name = (string as NSString).substring(with: nameRange)
let size = (string as NSString).substring(with: sizeRange)
print("\(name) - \(size)")
return (name, CGFloat((size as NSString).floatValue))
}
return nil
}
static func named(_ name : String) -> UIFont {
if let (font, size) = extract(name) {
switch font {
case "materialr":
return Material.regular(size: size)
//Add more cases
default:
break
}
}else{
print("Unable to parse \(name)")
}
return UIFont.systemFont(ofSize: 60)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment