Skip to content

Instantly share code, notes, and snippets.

View StanislavK's full-sized avatar
🚜
Working

StanislavK StanislavK

🚜
Working
View GitHub Profile
import UIKit
// Credits to https://github.com/JohnSundell/SwiftKit/blob/master/Source/iOS/ClosureButton.swift
/// A button that executes a closure when tapped
public class ClosureButton: UIButton {
private let closure: () -> Void
public init(frame: CGRect, closure: () -> Void) {
self.closure = closure
super.init(frame: frame)
public struct DeviceModel {
/// Returns true, if iPhoneX is detected
/// Detection is based on the portrait height, see https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/
public static var isiPhoneX: Bool = {
let iPhoneXPortaitHeight: CGFloat = 812
let currentDevicePortraitHeight: CGFloat = UIScreen.main.fixedCoordinateSpace.bounds.size.height
return currentDevicePortraitHeight == iPhoneXPortaitHeight ? true : false
}()
}
let searchController = UISearchController(searchResultsController: nil)
if #available(iOS 11.0, *) {
self.navigationItem.searchController = searchController
searchController.isActive = true
}
else {
present(searchController, animated: true, completion: nil)
}
extension String {
/** Get email addresses in a string, discard any other content. */
func emailAddresses() -> [String] {
var addresses = [String]()
if let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) {
let matches = detector.matches(in: self, options: [], range: NSMakeRange(0, self.count))
for match in matches {
if let matchURL = match.url,
let matchURLComponents = URLComponents(url: matchURL, resolvingAgainstBaseURL: false),
matchURLComponents.scheme == "mailto"
func printAllFontNames() {
let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName)
print("Font Names = [\(names)]")
}
}
//: Playground - noun: a place where people can play
import UIKit
// Computed property
// NOTE: Since a computed property gets reassigned upon every access!, it can only be declared as a var and not a let.
var urlSession: URLSession {
let urlSessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default.copy() as! URLSessionConfiguration
urlSessionConfiguration.requestCachePolicy = NSURLRequest.CachePolicy.returnCacheDataElseLoad
extension Bool {
mutating func toggle() {
self = !self
}
}
import UIKit
protocol ReusableView: class {}
extension ReusableView {
static var reuseIdentifier: String {
return String(describing: self)
}
}
import UIKit
protocol NibLoadableView: class { }
extension NibLoadableView {
static var nibName: String {
return String(describing: self)
}
import UIKit
extension UITableView {
func register<T: UITableViewCell>(_: T.Type) {
let nib = UINib(nibName: T.nibName, bundle: nil)
register(nib, forCellReuseIdentifier: T.reuseIdentifier)
}