Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / UIViewController+Embed.swift
Created January 25, 2020 22:59
UIViewController+Embed
extension UIViewController {
func embed(_ vc: UIViewController, in _containerView: UIView? = nil) {
let containerView: UIView = _containerView ?? view // Use the whole view if no container view is provided.
containerView.addSubview(vc.view)
NSLayoutConstraint.activate([
vc.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0),
vc.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0),
vc.view.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0),
vc.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0)
])
@vinczebalazs
vinczebalazs / UIColor+NamedColors.swift
Last active April 13, 2020 05:34
UIColor+NamedColors
extension UIColor {
static var accent: UIColor {
UIColor(named: "Accent")!
}
static var primary: UIColor {
UIColor(named: "Primary")!
}
static var primaryText: UIColor {
@vinczebalazs
vinczebalazs / NamedColorsExample.swift
Created January 25, 2020 23:15
Named colors example
UIColor(named: "Primary")
@vinczebalazs
vinczebalazs / CellExample.swift
Created January 25, 2020 23:18
Cell example
cell.item = items[indexPath.row]
@vinczebalazs
vinczebalazs / CornerRadiusPlayground.swift
Created February 9, 2020 22:30
A Playground to reproduce a cornerRadius anomality.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
final class Cell: UITableViewCell {
private let borderLayer = CAShapeLayer()
private let _backgroundView = UIView()
@vinczebalazs
vinczebalazs / uifontexmaple.swift
Created February 10, 2020 18:00
UIFont example
UIFont(name: "Quicksand-Bold", weight: 16)