Skip to content

Instantly share code, notes, and snippets.

View JasonCanCode's full-sized avatar

Jason JasonCanCode

View GitHub Profile
@JasonCanCode
JasonCanCode / LineGraphView.swift
Created July 26, 2016 19:11
An interactive line graph
import UIKit
typealias ConnectionDrawing = Void -> Void
class LineGraphView: UIView {
var data = [Double]()
var offset: CGFloat = 0.0
var shownRange = 0 ..< 0
var maximumValueShown: Double = 0.0
@JasonCanCode
JasonCanCode / UIView+Constraint.swift
Last active March 20, 2017 17:58
Checks the constraints of this view's superview, as well as all of it's subviews, for the constraint with the identifier passed.
import UIKit
extension UIView {
/// Checks the constraints of this view's superview, as well as all of it's subviews, for the constraint with the identifier passed.
func constraint(withIdentifier identifier: String) -> NSLayoutConstraint? {
if let constraint = superview?.constraintFiltered(identifier) {
return constraint
} else if let constraint = constraintFiltered(identifier) {
return constraint
@JasonCanCode
JasonCanCode / UILabel+AttributedString.swift
Last active March 20, 2017 18:00
Assigns attributed text to the label by iterating through an array of strings with associated FontTypes.
/// Used to identify the intended format of a string when using the `attributedTextFromArray:` func of a UILabel extension.
enum FontType {
case system, bold, italic, underlined
}
extension UILabel {
/**
Assigns attributed text to the label by iterating through an array of strings with associated FontTypes.
Example use:
@JasonCanCode
JasonCanCode / NibView.swift
Last active February 22, 2018 18:55
In the xib IB, make the used custom view class (subclass of NibView) the File’s Owner and connect the view outlet to the xib view.
import UIKit
/**
In the xib IB, make the used custom view class (subclass of NibView)
the File’s Owner and connect the view outlet to the xib view.
*/
class NibView: UIView {
@IBOutlet weak private var view: UIView!
var nibName: String {
@JasonCanCode
JasonCanCode / SwipeControl.swift
Last active March 20, 2017 18:25
Overview slides left and/or right to reveal actions. Sliding past a threshold will trigger a set action.
import UIKit
// NOTE: percentageThreshold is shared across all instances of SwipeControl
private var percentageThreshold: Int = 75
private enum Direction {
case left, right, none
}
@JasonCanCode
JasonCanCode / ArrayValidation.swift
Last active March 20, 2017 18:26
func validateArray
func validateArray<T>(_ array: [T?]) -> [T] {
var validElements: [T] = []
for case let element? in array {
validElements.append(element)
}
return validElements
}
@JasonCanCode
JasonCanCode / AlertHelper.swift
Last active August 3, 2018 15:52
A convenient way to create and present AlertControllers.
import UIKit
extension UIAlertAction {
convenience init(title: String, handler: ((UIAlertAction?) -> Void)? = nil) {
self.init(title: title, style: .default, handler: handler)
}
}
/// - Tag: AlertHelper
struct AlertHelper {
@JasonCanCode
JasonCanCode / AsyncImageLoader.swift
Last active May 12, 2022 17:25
Safely load and cache images asynchronously
import UIKit
// MARK: - ImageLoader Protocol
public typealias ImageLoaderHandler = (UIImage?, Error?) -> Void
public protocol ImageLoader {
func updateImage(fromURLString urlString: String?, placeholderImage: UIImage?, completionHandler: @escaping ImageLoaderHandler)
func imageFromCache(_ urlString: String?) -> UIImage?
}
@JasonCanCode
JasonCanCode / NibViewType.swift
Created March 20, 2017 18:10
Protocol used by a UIView subclass for xibs
import UIKit
/**
The adopting UIView is the File Ower of a nib by the same name. All IBOutlets are wired through the File Owner (not the content view itself) and the content view is also wired to the IBOutlet `view`.
NOTE - `loadFromNib:` must be called within a convenience initializer.
Keep in mind that any UIView instance method that you would normally use `self` should be called `view` instead.
*/
protocol NibViewType: UIAppearance {
@JasonCanCode
JasonCanCode / StoryboardHelper.swift
Last active November 30, 2018 18:33
Easily generate a new instance of a View Controller that exists within a storyboard
import UIKit
struct StoryboardHelper {
/// Assumes that the storyboard identifier for the View Controller class provided matches its name and that it has been added to the `storyboardNameForViewController` switch statement.
static func new<T>() -> T? {
let nibName = String(describing: T.self)
let storyboardName = storyboardNameForViewController(named: nibName)
return getViewController(named: nibName, fromStoryboardNamed: storyboardName) as? T
}