Skip to content

Instantly share code, notes, and snippets.

View marcos1262's full-sized avatar

Marcos Paulo marcos1262

View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@marcos1262
marcos1262 / UIColorExtension.swift
Last active October 27, 2018 14:07
Get UIColor from RGB or Hex value
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(rgb: Int) {
@marcos1262
marcos1262 / ButtonWithGradient.swift
Last active August 3, 2018 11:55
Example of gradient on iOS
override func draw(_ rect: CGRect) {
self.layer.insertSublayer(AppDelegate.mainGradient(self.frame.width, self.frame.height), at: 0)
}
@marcos1262
marcos1262 / NSManagedObjectExtension.swift
Created October 27, 2018 13:42
Nice functions for dealing with CoreData
//
// NSManagedObject.swift
//
// Created by Marcos Santos on 8/30/18.
// Copyright © 2018 Marcos Santos 2018. All rights reserved.
//
import Foundation
import CoreData
@marcos1262
marcos1262 / DataManager.swift
Created October 27, 2018 13:43
Wrapped CoreData stack
//
// DataManager.swift
//
// Created by Marcos Santos on 8/30/18.
// Copyright © 2018 Marcos Santos 2018. All rights reserved.
//
import CoreData
public class DataManager {
extension UIViewController {
func showError(_ message: String?) {
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
@marcos1262
marcos1262 / UIViewControllerExtension.swift
Last active October 27, 2018 14:06
Spinner indicator to put over loading views
extension UIViewController {
func displaySpinner(onView : UIView) -> UIView {
let spinnerView = UIView.init(frame: onView.bounds)
spinnerView.backgroundColor = UIColor.white //UIColor.black.withAlphaComponent(0.5)
let ai = UIActivityIndicatorView(style: .gray)
ai.startAnimating()
ai.center = spinnerView.center
DispatchQueue.main.async {
spinnerView.addSubview(ai)
@marcos1262
marcos1262 / UIImageViewExtension.swift
Last active October 27, 2018 14:06
Asynchronous download of images and saving in caches directory
extension UIImageView {
//Use this function with the imageview
func loadImageUsingCacheWithUrlString(_ url: String?) {
guard let url = url else {
self.image = UIImage(named: "default")
return
}
let imageName = url.split(separator: "/").last! as NSString
let fileManager = FileManager.default
@marcos1262
marcos1262 / MyModel.swift
Created October 27, 2018 14:11
Abbreviated date interval String formatting
var timeString: String {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.month, .day, .hour, .minute]
formatter.unitsStyle = .abbreviated
formatter.zeroFormattingBehavior = .dropAll
if let date = self.date, let interval = formatter.string(from: date, to: Date()) {
return "\(interval) ago"
}
return "unknown"