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 Comparable { | |
func clamped(min: Self, max: Self) -> Self { | |
if self < min { | |
return min | |
} | |
if self > max { | |
return max | |
} | |
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 UITextView { | |
func makeSelfSizing() -> [NSKeyValueObservation] { | |
self.isScrollEnabled = false | |
NotificationCenter.default.addObserver(self, selector: #selector(didChangeTextViewContent), name: .UITextViewTextDidChange, object: self) | |
return [ | |
observe(\UITextView.text, changeHandler: { this, change in | |
self.didChangeTextViewContent() | |
}), | |
observe(\UITextView.attributedText, changeHandler: { this, change in |
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 UITextView { | |
var currentWord: String? { | |
return currentWordRange.map({ String(text[$0]) }) | |
} | |
var currentWordRange: Range<String.Index>? { | |
let regex = try! NSRegularExpression(pattern: "\\S+$") | |
let textRange = NSRange(location: 0, length: selectedRange.location) | |
if let range = regex.firstMatch(in: text, range: textRange)?.range { | |
return Range(range, in: text) |
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
// | |
// TextViewFormatter.swift | |
// TextViewFormatter | |
// | |
// Created by Daniel Esteban Cardona Rojas on 3/6/19. | |
// Copyright © 2019 Daniel Esteban Cardona Rojas. All rights reserved. | |
// | |
import UIKit | |
/* |
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 UserDefaults { | |
@discardableResult | |
func serialize<V: Codable>(_ value: V, forKey key: String) -> Bool { | |
if let data = value.asPlistData { | |
self.set(data, forKey: key) | |
return self.synchronize() | |
} | |
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
import PromiseKit | |
extension APIClient { | |
func request<Response, T>(_ requestConvertible: T, | |
additionalHeaders headers: [String: String]? = nil, | |
additionalQuery queryParameters: [String: String]? = nil, | |
baseUrl: URL? = nil) -> Promise<T.Result> | |
where T: URLResponseCapable, T: URLRequestConvertible, T.Result == Response { | |
return Promise { seal in | |
self.request(requestConvertible, additionalHeaders: headers, additionalQuery: queryParameters, success: { response in |
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 UIEdgeInsets { | |
static func with(_ constant: CGFloat) -> UIEdgeInsets { | |
return UIEdgeInsets(top: constant, left: constant, bottom: constant, right: constant) | |
} | |
} | |
extension Comparable { | |
func clamp(min: Self, max: Self) -> Self { | |
if self < min { | |
return min |
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 UIScrollView { | |
func scrollSubViewToTop(_ subview: UIView, offset: CGFloat, animated: Bool) { | |
let point = convert(subview.frame.origin, from: subview.superview ?? subview) | |
setContentOffset(CGPoint(x: 0, y: point.y - offset), animated: animated) | |
} | |
func viewPortOffset(of subview: UIView) -> CGFloat { | |
let point = convert(subview.frame.origin, from: subview.superview ?? subview) | |
return point.y - contentOffset.y | |
} |
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 String { | |
func isContainedBy(set: CharacterSet) -> Bool { | |
let letters = self.components(separatedBy: set) | |
print(letters) | |
return self.count == letters.count | |
} | |
} |
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 String { | |
func isContainedBy(set: CharacterSet) -> Bool { | |
let letters = self.components(separatedBy: set) | |
print(letters) | |
return self.count == letters.count | |
} | |
func filtered(by set: CharacterSet) -> String { | |
let chars = self.unicodeScalars.filter({ set.contains($0)}) | |
let unicode = UnicodeScalarView(chars) |