Skip to content

Instantly share code, notes, and snippets.

@pxpgraphics
Created July 29, 2014 00:11
Show Gist options
  • Save pxpgraphics/f09a4b3545094dab5042 to your computer and use it in GitHub Desktop.
Save pxpgraphics/f09a4b3545094dab5042 to your computer and use it in GitHub Desktop.
Swift extensions for common occurances
//
// ArrayExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/8/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
extension Array {
// MARK: - Index helpers
var firstObject: T {
return self[startIndex]
}
var lastObject: T {
return self[endIndex - 1]
}
}
//
// CoreGraphicsExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/28/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import CoreGraphics
extension CGRect {
// MARK: - Dimesion helper functions
var x: CGFloat {
get {
return origin.x
}
}
var y: CGFloat {
get {
return origin.y
}
}
var center: CGPoint {
get {
return CGPointMake(width / 2.0, height / 2.0)
}
}
}
//
// NSFileManagerExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/26/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
extension NSFileManager {
// MARK: - Directory helper functions
class func applicationDocumentDirectory() -> NSURL {
return (NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).lastObject) as NSURL
}
}
//
// StringExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/8/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
extension String {
// MARK: - String helpers
var length: Int {
get {
return countElements(self)
}
}
// MARK: - Sting comparison functions
func containsStrings(strings: Array <String>) -> Bool {
for string in strings {
// println("SELF: \(self), STRING: \(string)")
var subString = self as NSString
if subString.containsString(string) {
return true
}
}
return false
}
func containsStringsIgnoringCase(strings: Array <String>) -> Bool {
for string in strings {
// println("SELF: \(self.lowercaseString), STRING: \(string)")
var subString = self.lowercaseString as NSString
if subString.containsString(string) {
return true
}
}
return false
}
}
//
// UIColorExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/20/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
import UIKit
extension UIColor {
// MARK: - HEX value functions
convenience init(hex: Int, alpha: CGFloat = 1.0) {
let maxColorValue: CGFloat = 255.0
let red = CGFloat((hex & 0xFF0000) >> 16) / maxColorValue
let green = CGFloat((hex & 0xFF00) >> 8) / maxColorValue
let blue = CGFloat((hex & 0xFF)) / maxColorValue
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
}
//
// UIControlExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/20/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
import UIKit
extension UIControl {
// MARK: - Control helper functions
func disable() {
enabled = false
}
func enable() {
enabled = true
}
}
//
// UIImageExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/26/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
import UIKit
extension UIImage {
// MARK: - Color to image function
class func imageFromColor(color: UIColor) -> UIImage {
let imageRect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(imageRect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, imageRect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
//
// UIKitExtension.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/20/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
import UIKit
class UIConstants {
// MARK: - Screen dimension helpers
class var screenBounds: CGRect {
get {
return UIScreen.mainScreen().applicationFrame
}
}
class var screenCenter: CGPoint {
get {
return UIScreen.mainScreen().applicationFrame.center
}
}
class var screenWidth: CGFloat {
get {
return UIScreen.mainScreen().applicationFrame.width
}
}
class var screenHeight: CGFloat {
get {
return UIScreen.mainScreen().applicationFrame.height
}
}
// MARK: - Status bar helpers
class var statusBarHeight: CGFloat {
get {
return UIApplication.sharedApplication().statusBarFrame.height
}
}
class var statusBarHidden: Bool {
get {
return UIApplication.sharedApplication().statusBarHidden
}
set (hidden) {
UIApplication.sharedApplication().statusBarHidden = hidden
}
}
}
//
// UINavigationBarExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/26/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
import UIKit
extension UINavigationBar {
// MARK: - Hairline image view functions
func hideBottomHairline() {
let navigationBarImageView = hairlineImageViewInNavigationBar(self)
navigationBarImageView!.hidden = true
}
func showBottomHairline() {
let navigationBarImageView = hairlineImageViewInNavigationBar(self)
navigationBarImageView!.hidden = false
}
private func hairlineImageViewInNavigationBar(view: UIView) -> UIImageView? {
if view.isKindOfClass(UIImageView) && view.bounds.height <= 1.0 {
return (view as UIImageView)
}
let subviews = (view.subviews as [UIView])
for subview: UIView in subviews {
if let imageView: UIImageView = hairlineImageViewInNavigationBar(subview)? {
return imageView
}
}
return nil
}
}
//
// UITableViewExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/20/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
import UIKit
extension UITableView {
// MARK: - Scroll functions
func scrollToBottom(animated: Bool = true) {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.height)
setContentOffset(bottomOffset, animated: animated);
}
}
//
// UIToolbarExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/26/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
import UIKit
extension UIToolbar {
// MARK: - Hairline image view functions
func hideHairline() {
let navigationBarImageView = hairlineImageViewInToolbar(self)
navigationBarImageView!.hidden = true
}
func showHairline() {
let navigationBarImageView = hairlineImageViewInToolbar(self)
navigationBarImageView!.hidden = false
}
private func hairlineImageViewInToolbar(view: UIView) -> UIImageView? {
if view.isKindOfClass(UIImageView) && view.bounds.height <= 1.0 {
return (view as UIImageView)
}
let subviews = (view.subviews as [UIView])
for subview: UIView in subviews {
if let imageView: UIImageView = hairlineImageViewInToolbar(subview)? {
return imageView
}
}
return nil
}
}
//
// UIViewExtensions.swift
// MyDailyGrind
//
// Created by Paris Pinkney on 7/20/14.
// Copyright (c) 2014 PXPGraphics. All rights reserved.
//
import Foundation
extension UIView {
// MARK: - Center views
func centerHorizontally(parentWidth: CGFloat) {
let xCenter = CGFloat(floor(Double(CGFloat(parentWidth - frame.width) / CGFloat(2.0))))
self.frame = CGRect(x: xCenter, y: frame.origin.y, width: frame.width, height: frame.height)
}
// MARK: - Convert view to image
func viewToImage() -> (image: UIImage, filePath: NSURL) {
let image = viewToImageWithScale(0.0).image
let filePath = viewToImageWithScale(0.0).filePath
return (image, filePath)
}
func viewToImageWithScale(scale: CGFloat) -> (image: UIImage, filePath: NSURL) {
let image = viewToImageWithScale(scale, legacy: false).image
let filePath = viewToImageWithScale(scale, legacy: false).filePath
return (image, filePath)
}
func viewToImageWithScale(scale: CGFloat, legacy: Bool) -> (image: UIImage, filePath: NSURL) {
// If scale is 0.0, it'll follow the screen's scale for creating the bounds.
UIGraphicsBeginImageContextWithOptions(bounds.size, false, scale)
if legacy {
layer.renderInContext(UIGraphicsGetCurrentContext())
} else {
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
}
// Get the image out of the context.
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Save the image to disk.
let documentsDirectory = NSFileManager.applicationDocumentDirectory()
var timestamp = "\(NSDate.date())"
let characterSet = NSCharacterSet.alphanumericCharacterSet().invertedSet
let components = (timestamp.componentsSeparatedByCharactersInSet(characterSet) as NSArray)
timestamp = components.componentsJoinedByString("-")
var path = documentsDirectory.path.stringByAppendingPathComponent("mydailygrind-img-\(timestamp).png.ig")
let imageData = UIImagePNGRepresentation(image)
imageData.writeToFile(path, atomically: true)
let filePath = NSURL.fileURLWithPath(path)
// Return the result.
return (image, filePath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment