Skip to content

Instantly share code, notes, and snippets.

@vinczebalazs
vinczebalazs / color.m
Created May 12, 2016 20:36 — forked from kylefox/color.m
Generate a random color (UIColor) in Objective-C
/*
Distributed under The MIT License:
http://opensource.org/licenses/mit-license.php
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
@vinczebalazs
vinczebalazs / UICollectionViewLeftAlignedLayout.swift
Created October 6, 2019 15:33
Left aligned collection view flow layout.
// MIT License
//
// Copyright (c) 2019 Balázs Vincze
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
final class ExpandableLabel: UILabel {
private let collapsedHeight: CGFloat = 65
private var heightConstraint: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
heightConstraint = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil,
attribute: .height, multiplier: 1, constant: collapsedHeight)
NSLayoutConstraint.activate([heightConstraint])
@vinczebalazs
vinczebalazs / LoadingButton.swift
Last active January 29, 2020 12:27
A UIButton subclass which can display a loading spinner instead of its title.
import UIKit
class LoadingButton: UIButton {
// MARK: Public Properties
var spinnerSpeed = 0.75
var spinnerWidth: CGFloat = 4
var spinnerRadius = 30
var isSpinnerRounded = true
@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
@vinczebalazs
vinczebalazs / SheetModalPresentationController.swift
Last active February 1, 2024 04:40
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
interactor.completionSpeed = max(1, velocity / (gestureView.frame.height * (1 / interactor.duration)))
let presentationController = SheetModalPresentationController(presentedViewController: vc,
presenting: self,
height: height)
vc.transitioningDelegate = presentationController
vc.modalPresentationStyle = .custom
present(vc, animated: true)
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)
}
@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]
}
}