Skip to content

Instantly share code, notes, and snippets.

@mosluce
Last active March 1, 2020 11:51
Show Gist options
  • Save mosluce/5416a276e504423afce5a2f18e9b4ddd to your computer and use it in GitHub Desktop.
Save mosluce/5416a276e504423afce5a2f18e9b4ddd to your computer and use it in GitHub Desktop.
超惰性 extension 大全 + 默司微調
//
// NSObject.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
extension NSObject {
class var className: String {
return String(describing: self)
}
var className: String {
return type(of: self).className
}
}
//
// String.swift
// QuickbloxDemo2
//
// Created by 默司 on 2016/12/10.
// Copyright © 2016年 默司. All rights reserved.
//
import UIKit
extension String {
var url: URL? {
return URL(string: self)
}
func bounds(for width: CGFloat, font: UIFont) -> CGRect {
return (self as NSString).boundingRect(with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude), options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName: font], context: nil)
}
}
//
// Comparable.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
extension UIAlertController {
convenience init(title: String?, message: String?) {
self.init(title: title, message: message, preferredStyle: .alert)
}
func addAction(title: String, style: UIAlertActionStyle = .default, handler: ((UIAlertAction) -> Void)? = nil) -> Self {
let okAction = UIAlertAction(title: title, style: style, handler: handler)
addAction(okAction)
return self
}
func addActionWithTextFields(title: String, style: UIAlertActionStyle = .default, handler: ((UIAlertAction, [UITextField]) -> Void)? = nil) -> Self {
let okAction = UIAlertAction(title: title, style: style) { [weak self] action in
handler?(action, self?.textFields ?? [])
}
addAction(okAction)
return self
}
func configureForIPad(sourceRect: CGRect, sourceView: UIView? = nil) -> Self {
popoverPresentationController?.sourceRect = sourceRect
if let sourceView = UIApplication.shared.topViewController?.view {
popoverPresentationController?.sourceView = sourceView
}
return self
}
func configureForIPad(barButtonItem: UIBarButtonItem) -> Self {
popoverPresentationController?.barButtonItem = barButtonItem
return self
}
func addTextField(handler: @escaping (UITextField) -> Void) -> Self {
addTextField(configurationHandler: handler)
return self
}
func present() {
UIApplication.shared.topViewController?.present(self, animated: true, completion: nil)
}
func dismiss() {
self.dismiss(animated: true)
}
}
//
// UIApplication.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
extension UIApplication {
var topViewController: UIViewController? {
guard var topViewController = UIApplication.shared.keyWindow?.rootViewController else { return nil }
while let presentedViewController = topViewController.presentedViewController {
topViewController = presentedViewController
}
return topViewController
}
var topNavigationController: UINavigationController? {
return topViewController as? UINavigationController
}
}
//
// UICollectionView.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
extension UICollectionView {
func register<T: UICollectionViewCell>(cellType: T.Type) {
let className = cellType.className
let nib = UINib(nibName: className, bundle: nil)
register(nib, forCellWithReuseIdentifier: className)
}
func register<T: UICollectionViewCell>(cellTypes: [T.Type]) {
cellTypes.forEach { register(cellType: $0) }
}
func register<T: UICollectionReusableView>(reusableViewType: T.Type, of kind: String = UICollectionElementKindSectionHeader) {
let className = reusableViewType.className
let nib = UINib(nibName: className, bundle: nil)
register(nib, forSupplementaryViewOfKind: kind, withReuseIdentifier: className)
}
func register<T: UICollectionReusableView>(reusableViewTypes: [T.Type], kind: String = UICollectionElementKindSectionHeader) {
reusableViewTypes.forEach { register(reusableViewType: $0, of: kind) }
}
func dequeueReusableCell<T: UICollectionViewCell>(with type: T.Type, for indexPath: IndexPath) -> T {
return dequeueReusableCell(withReuseIdentifier: type.className, for: indexPath) as! T
}
func dequeueReusableView<T: UICollectionReusableView>(with type: T.Type, for indexPath: IndexPath, of kind: String = UICollectionElementKindSectionHeader) -> T {
return dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: type.className, for: indexPath) as! T
}
}
//
// UIColor.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
extension UIColor {
convenience init(hex: Int, alpha: Double = 1.0) {
let r = CGFloat((hex & 0xFF0000) >> 16) / 255.0
let g = CGFloat((hex & 0x00FF00) >> 8) / 255.0
let b = CGFloat(hex & 0x0000FF) / 255.0
self.init(red: r, green: g, blue: b, alpha: CGFloat(alpha))
}
func lighter(by percentage: CGFloat = 10.0) -> UIColor? {
return self.adjust(by: abs(percentage))
}
func darker(by percentage: CGFloat = 10.0) -> UIColor? {
return self.adjust(by: -1 * abs(percentage))
}
func adjust(by percentage:CGFloat = 10.0) -> UIColor? {
var r:CGFloat = 0, g:CGFloat = 0, b:CGFloat = 0, a:CGFloat = 0;
if self.getRed(&r, green: &g, blue: &b, alpha: &a) {
return UIColor(red: min(r + percentage/100, 1.0),
green: min(g + percentage/100, 1.0),
blue: min(b + percentage/100, 1.0),
alpha: a)
} else {
return nil
}
}
}
//
// UIimage.swift
// Gotyou
//
// Created by 默司 on 2016/12/1.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
extension UIImage {
static func image(named: String, in bundle: Bundle? = Bundle(identifier: "com.gotyou.app")) -> UIImage {
return UIImage(named: named, in: bundle, compatibleWith: nil)!
}
func resize(max: CGFloat) -> UIImage? {
var image: UIImage?
if self.size.width > self.size.height {
let scale = max / self.size.width
let height = self.size.height * scale
UIGraphicsBeginImageContext(CGSize(width: max, height: height))
draw(in: CGRect(x: 0, y: 0, width: max, height: height))
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
} else {
let scale = max / self.size.width
let width = self.size.width * scale
UIGraphicsBeginImageContext(CGSize(width: width, height: max))
draw(in: CGRect(x: 0, y: 0, width: width, height: max))
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}
return image
}
}
//
// UINavigationController.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
extension UINavigationController {
static var `default`: UINavigationController? {
return UIApplication.shared.topNavigationController
}
func push<T: UIViewController>(vcClass: T.Type, storyboard: UIStoryboard = .main) {
self.pushViewController(storyboard.instantiateViewController(withIdentifier: String(describing: vcClass)), animated: true)
}
}
//
// UIStoryboard.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
extension UIStoryboard {
static let main = UIStoryboard(name: "Main", bundle: Bundle.main)
}
//
// UITableView.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
extension UITableView {
func register<T: UITableViewCell>(cellType: T.Type) {
let className = cellType.className
let nib = UINib(nibName: className, bundle: nil)
register(nib, forCellReuseIdentifier: className)
}
func register<T: UITableViewCell>(cellTypes: [T.Type]) {
cellTypes.forEach { register(cellType: $0) }
}
func dequeueReusableCell<T: UITableViewCell>(with type: T.Type, for indexPath: IndexPath) -> T {
return self.dequeueReusableCell(withIdentifier: type.className, for: indexPath) as! T
}
}
//
// UIView.swift
// QBDemo01
//
// Created by 默司 on 2016/12/2.
// Copyright © 2016年 默司. All rights reserved.
//
import Foundation
import UIKit
protocol NibInstantiatable {}
extension UIView: NibInstantiatable {}
extension NibInstantiatable where Self: UIView {
/// XIBのViewを生成
///
/// - Parameter ownerOrNil:
/// - Returns:
static func instantiate(withOwner ownerOrNil: Any? = nil) -> Self {
let nib = UINib(nibName: self.className, bundle: nil)
return nib.instantiate(withOwner: ownerOrNil, options: nil)[0] as! Self
}
}
extension UIView {
/// 子Viewを全て削除
func removeAllSubviews() {
subviews.forEach {
$0.removeFromSuperview()
}
}
}
extension UIView {
/// スクショを撮る
///
/// - Parameter width:
/// - Returns:
func screenShot(width: CGFloat) -> UIImage? {
let imageBounds = CGRect(x: 0, y: 0, width: width, height: bounds.size.height * (width / bounds.size.width))
UIGraphicsBeginImageContextWithOptions(imageBounds.size, true, 0)
drawHierarchy(in: imageBounds, afterScreenUpdates: true)
var image: UIImage?
let contextImage = UIGraphicsGetImageFromCurrentImageContext()
if let contextImage = contextImage, let cgImage = contextImage.cgImage {
image = UIImage(
cgImage: cgImage,
scale: UIScreen.main.scale,
orientation: contextImage.imageOrientation
)
}
UIGraphicsEndImageContext()
return image
}
}
//
// UIViewController.swift
// Gotyou
//
// Created by 默司 on 2016/12/5.
// Copyright © 2016年 默司. All rights reserved.
//
import UIKit
protocol StoryBoardInstantiatable {}
extension UIViewController : StoryBoardInstantiatable {}
extension StoryBoardInstantiatable where Self: UIViewController {
static func instantiate(fromStoryboard storyboard: UIStoryboard = .main) -> Self {
return storyboard.instantiateViewController(withIdentifier: self.className) as! Self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment