Skip to content

Instantly share code, notes, and snippets.

View efremidze's full-sized avatar
👨‍💻

Lasha Efremidze efremidze

👨‍💻
View GitHub Profile
@efremidze
efremidze / characterCount.swift
Last active June 1, 2017 23:46
String character count (more accurate)
extension String {
var count: Int {
var length = 0
enumerateSubstrings(in: startIndex..<endIndex, options: .byComposedCharacterSequences) { substring, substringRange, enclosingRange, stop in
length += 1
}
return length
}
}
extension Dictionary where Value == Any {
func value<T>(forKey key: Key, defaultValue: @autoclosure () -> T) -> T {
guard let value = self[key] as? T else { return defaultValue() }
return value
}
}
protocol EnumStringConvertible: CustomStringConvertible {}
extension EnumStringConvertible {
var description: String { return String(reflecting: self) }
}
protocol EnumUserDefaultsConvertible: EnumStringConvertible {}
extension EnumUserDefaultsConvertible {
var key: String { return description }
var value: Any? {
get { return UserDefaults.standard[key] }
private var targets = [Target]()
extension UIGestureRecognizer {
convenience init(action: @escaping (UIGestureRecognizer) -> ()) {
self.init()
addAction(action)
}
func addAction(_ action: @escaping (UIGestureRecognizer) -> ()) {
@efremidze
efremidze / navigationBarStyling.swift
Created May 18, 2017 01:31
Styling UINavigationBar iOS 9/10
// barTintColor will apply color for the app's navigation bar
UINavigationBar.appearance().barTintColor = .blue
// backgroundColor will apply color for some of the sharing container's app except for Messages app
UINavigationBar.appearance().backgroundColor = .blue
// backgroundImage will apply the color image for navigation bar for most of the sharing container's except for `Notes`
UINavigationBar.appearance().setBackgroundImage(UIImage(color: .blue), for: .default)
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
func delay(_ seconds: Int, _ queue: DispatchQueue = .main, _ closure: @escaping () -> Void) {
let time = DispatchTime.now() + .seconds(seconds)
queue.asyncAfter(deadline: time, execute: closure)
}
// preprocessor macros for logging
#define DefaultLog [NSString stringWithFormat:@"%s (line %d)", __PRETTY_FUNCTION__, __LINE__]
#define LogWarn(format, ...) NSLog((@"%@ -> WARNING: " format), DefaultLog, ##__VA_ARGS__)
#define LogError(format, ...) NSLog((@"%@ -> ERROR: " format), DefaultLog, ##__VA_ARGS__)
extension UIButton {
func addSpringAnimation() {
addTarget(self, action: #selector(scaleDown), for: [.touchDown, .touchDragEnter])
addTarget(self, action: #selector(scaleUp), for: [.touchUpInside, .touchDragExit, .touchCancel])
}
@IBAction func scaleDown(_ button: UIButton) {
UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: {
button.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
extension Dictionary {
subscript(key: Key, `default` value: Value) -> Value {
mutating get {
return self[key] ?? {
self[key] = value
return value
}()
}
set { self[key] = newValue }
import Typist
protocol KeyboardAware {
var keyboard: Typist { get }
var contentInset: UIEdgeInsets { get }
var scrollIndicatorInsets: UIEdgeInsets { get }
}
extension KeyboardAware where Self: UIScrollView {
func observeKeyboard() {