Created
July 29, 2014 00:11
-
-
Save pxpgraphics/f09a4b3545094dab5042 to your computer and use it in GitHub Desktop.
Swift extensions for common occurances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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