Skip to content

Instantly share code, notes, and snippets.

@vinczebalazs
vinczebalazs / UIViewSuperview.swift
Last active January 25, 2020 22:47
UIView+Superview
extension UIView {
func superview<T>(of type: T.Type) -> T? {
return superview as? T ?? superview.flatMap { $0.superview(of: type) }
}
}
// Example usage:
func textFieldDidBeginEditing(_ textField: UITextField) {
// Get the cell containing the textfield.
@vinczebalazs
vinczebalazs / NSAttributedStringHighlight.swift
Last active June 29, 2020 07:19
NSAttributedString+Highlight
extension NSAttributedString {
func highlighting(_ substring: String, using color: UIColor) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: self)
attributedString.addAttribute(.foregroundColor, value: color, range: (self.string as NSString).range(of: substring))
return attributedString
}
}
// Usage:
@vinczebalazs
vinczebalazs / UIButtonPadding.swift
Created January 25, 2020 22:32
UIButton+Padding
extension CGSize {
func addingPadding(width: CGFloat, height: CGFloat) -> CGSize {
CGSize(width: self.width + width, height: self.height + height)
}
}
// Usage:
class PaddedButton: UIButton {
override var intrinsicContentSize: CGSize {
@vinczebalazs
vinczebalazs / Array + IndexPath.swift
Last active January 25, 2020 22:27
Array + IndexPath subscript.
extension Array {
subscript(indexPath: IndexPath) -> Element {
self[indexPath.row]
}
}
@vinczebalazs
vinczebalazs / Array+IndexPath.swift
Created January 25, 2020 20:07
Subscript Array with IndexPath
import Foundation
extension Array {
subscript(indexPath: IndexPath) -> Element {
self[indexPath.row]
}
}
extension UIViewController {
func presentAsSheet(_ vc: UIViewController, height: CGFloat) {
let presentationController = SheetModalPresentationController(presentedViewController: vc,
presenting: self,
height: height)
vc.transitioningDelegate = presentationController
vc.modalPresentationStyle = .custom
present(vc, animated: true)
}
let presentationController = SheetModalPresentationController(presentedViewController: vc,
presenting: self,
height: height)
vc.transitioningDelegate = presentationController
vc.modalPresentationStyle = .custom
present(vc, animated: true)
interactor.completionSpeed = max(1, velocity / (gestureView.frame.height * (1 / interactor.duration)))
@vinczebalazs
vinczebalazs / SheetModalPresentationController.swift
Last active July 21, 2024 15:36
A presentation controller to use for presenting a view controller modally, which can be dismissed by a pull down gesture. The presented view controller's height is also adjustable.
import UIKit
extension UIView {
var allSubviews: [UIView] {
subviews + subviews.flatMap { $0.allSubviews }
}
func firstSubview<T: UIView>(of type: T.Type) -> T? {
allSubviews.first { $0 is T } as? T
@vinczebalazs
vinczebalazs / LoadingTextField.swift
Last active January 29, 2020 12:27
A UITextField subclass with the ability to show a loading spinner in its right view.
import UIKit
class LoadingTextField: UITextField {
// MARK: Public Properties
var spinnerSpeed = 0.75
var spinnerWidth: CGFloat = 3
var spinnerDiameter: CGFloat = 18
var isSpinnerRounded = true