Created
July 31, 2017 07:09
-
-
Save marclove/90e38c97de36fa2febadbaeaca5870de to your computer and use it in GitHub Desktop.
UIKit & Foundation Extensions
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
extension Date { | |
func add(_ value: Int, _ component: Calendar.Component) -> Date! { | |
let calendar = Calendar.current | |
return calendar.date(byAdding: component, value: value, to: self) | |
} | |
func subtract(_ value: Int, _ component: Calendar.Component) -> Date! { | |
let calendar = Calendar.current | |
return calendar.date(byAdding: component, value: -value, to: self) | |
} | |
private func gregorianOnly(closure: (_ calendar: Calendar) -> Date!) -> Date { | |
let calendar = Calendar.current | |
switch calendar.identifier { | |
case .gregorian: | |
return closure(calendar) | |
default: | |
print("Calculation intended for use only with a Gregorian calendar") | |
return closure(Calendar(identifier: .gregorian)) | |
} | |
} | |
var beginningOfYear: Date! { | |
return gregorianOnly { calendar in | |
let currentMonthComponents = calendar.dateComponents([.year], from: self) | |
return calendar.date(from: currentMonthComponents) | |
} | |
} | |
var endOfYear: Date! { | |
return beginningOfYear.add(1, .year).subtract(1, .second) | |
} | |
var beginningOfMonth: Date! { | |
return gregorianOnly { calendar in | |
let currentMonthComponents = calendar.dateComponents([.year, .month], from: self) | |
return calendar.date(from: currentMonthComponents) | |
} | |
} | |
var endOfMonth: Date! { | |
return beginningOfMonth.add(1, .month).subtract(1, .second) | |
} | |
var beginningOfWeek: Date! { | |
return gregorianOnly { calendar in | |
var currentDateComponents = calendar.dateComponents([.year, .weekOfYear], from: self) | |
currentDateComponents.weekday = 1 | |
return calendar.date(from: currentDateComponents) | |
} | |
} | |
var endOfWeek: Date! { | |
return beginningOfWeek.add(1, .weekOfYear).subtract(1, .second) | |
} | |
var beginningOfDay: Date! { | |
return gregorianOnly { calendar in | |
let currentDateComponents = calendar.dateComponents([.year, .month, .day], from: self) | |
return calendar.date(from: currentDateComponents) | |
} | |
} | |
var endOfDay: Date! { | |
return beginningOfDay.add(1, .day).subtract(1, .second) | |
} | |
var beginningOfHour: Date! { | |
return gregorianOnly { calendar in | |
let currentDateComponents = calendar.dateComponents([.year, .month, .day, .hour], from: self) | |
return calendar.date(from: currentDateComponents) | |
} | |
} | |
var endOfHour: Date! { | |
return beginningOfHour.add(1, .hour).subtract(1, .second) | |
} | |
var midday: Date! { | |
return gregorianOnly { calendar in | |
var currentDateComponents = calendar.dateComponents([.year, .month, .day], from: self) | |
currentDateComponents.hour = 12 | |
return calendar.date(from: currentDateComponents) | |
} | |
} | |
var noon: Date! { | |
return midday | |
} | |
var midnight: Date! { | |
return beginningOfDay | |
} | |
var yesterday: Date! { | |
return subtract(1, .day) | |
} | |
var tomorrow: Date! { | |
return add(1, .day) | |
} | |
} |
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
// Interesting Libraries: | |
// https://github.com/yannickl/DynamicColor | |
// https://github.com/SwiftGen/SwiftGen | |
// Custom Colors | |
extension UIColor { | |
static let charcoal = UIColor(red: 0.05, green: 0.05, blue: 0.05, alpha: 1.0) | |
static let mint = UIColor(red: 0.5, green: 0.9, blue: 0.7, alpha: 1.0) | |
} | |
// Convenience initializers with terser RGB & RGBA syntax | |
extension UIColor { | |
convenience init(rgb: Int...) { | |
let redPart: CGFloat = CGFloat(rgb[0]) / 255 | |
let greenPart: CGFloat = CGFloat(rgb[1]) / 255 | |
let bluePart: CGFloat = CGFloat(rgb[2]) / 255 | |
self.init(red: redPart, green: greenPart, blue: bluePart, alpha: CGFloat(1.0)) | |
} | |
convenience init(rgba: Int...) { | |
let redPart: CGFloat = CGFloat(rgba[0]) / 255 | |
let greenPart: CGFloat = CGFloat(rgba[1]) / 255 | |
let bluePart: CGFloat = CGFloat(rgba[2]) / 255 | |
let alphaPart: CGFloat = CGFloat(rgba[3]) / 100 | |
self.init(red: redPart, green: greenPart, blue: bluePart, alpha: alphaPart) | |
} | |
} |
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
extension UIImage { | |
static func from(color: UIColor, size: CGSize? = nil) -> UIImage? { | |
let defaultSize = CGSize(width: 1, height: 1) | |
let rect = CGRect(origin: .zero, size: (size ?? defaultSize)) | |
// Start drawing context; defer end | |
UIGraphicsBeginImageContext(rect.size) | |
defer { UIGraphicsEndImageContext() } | |
// Fill with given color | |
guard let context = UIGraphicsGetCurrentContext() else { return nil } | |
context.setFillColor(color.cgColor) | |
context.fill(rect) | |
return UIGraphicsGetImageFromCurrentImageContext() | |
} | |
} |
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
extension UIImageView { | |
static func from(color: UIColor, size: CGSize? = nil) -> UIImageView { | |
let defaultSize = CGSize(width: 1, height: 1) | |
let image = UIImage.from(color: color, size: (size ?? defaultSize)) | |
return UIImageView(image: image) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment