Skip to content

Instantly share code, notes, and snippets.

View HitTestOverride.swift
// Put this in a UITableViewCell or UICollectionViewCell subclass.
// Returns nil if point is not within the specified subviews.
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let subviews = [imageView, titleLabel, detailLabel, downloadButton]
for view in subviews {
if view.convert(view.bounds, to: self).contains(point) {
return super.hitTest(point, with: event)
}
}
return nil
View Dynamic cell height in UICollectionViewCompositionalLayout.swift
let layout = UICollectionViewCompositionalLayout(sectionProvider: {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(30))
View CompositionalLayoutCenterAlign-1.swift
let layout = UICollectionViewCompositionalLayout(sectionProvider: {
(sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
// (1) setting widthDimension to half of collectionView size
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.5),
heightDimension: .absolute(50))
@hlung
hlung / How to connect a PS3 controller.md
Last active Apr 12, 2022
How to connect PS3 controller on Mac OSX, PC
View How to connect a PS3 controller.md

How to connect PS3 controller on Mac OSX, PC, etc.

This is how you connect PS3 controller to Mac OSX, PC, etc. when previously connected to a PS3. You will need a Mini USB cable. Overcome your laziness, get up of your chair, and go get one!

A big misconception is that keep holding PS button will reset the controller's pairing. It DOES NOT! From my testings, the controller keeps paring with the last machine it was CONNECTED VIA A USB CABLE.

Here are the steps:

View medium-codable-lossyarray.swift
struct LossyArray<Element: Decodable>: Decodable {
private(set) var elements: [Element]
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
var elements = [Element]()
if let count = container.count {
elements.reserveCapacity(count)
}
@hlung
hlung / APIClient.swift
Last active Feb 21, 2022
APIClient startDecodableRequest
View APIClient.swift
import Foundation
class APIClient {
// 1 - Error enum
enum APIClientError: Error {
case dataTaskFailed(Error)
case noHTTPURLResponse
case badHTTPStatusCode(HTTPURLResponse)
case noData
View vimium-dark.css
div > .vimiumHintMarker {
/* linkhint boxes */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#565756),
color-stop(100%,#242524));
border: 1px solid #e4e5e4;
opacity: 1.0;
text-shadow: none !important;
}
div > .vimiumHintMarker span {
@hlung
hlung / PlaceholderTextView.swift
Created Nov 27, 2018
A UITextView subclass with placeholder text support. Swift 4.2
View PlaceholderTextView.swift
import UIKit
/// A UITextView subclass with placeholder text support.
/// It uses another UILabel to show the placeholder, shown when text is empty.
class PlaceholderTextView: UITextView {
lazy var placeholderLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(white: 0.5, alpha: 0.85)
label.backgroundColor = .clear
@hlung
hlung / UIColor+PXExtensions.h
Last active Dec 30, 2020
Make UIColor support hex color codes
View UIColor+PXExtensions.h
//
// UIColor+PXExtensions.h
// Creates UIColor from hex string. "#" prefix is optional. Supports 3 and 6 hex digits.
// Originally from http://pixelchild.com.au/post/12785987198/how-to-convert-a-html-hex-string-into-uicolor-with
// But that link seems to be broken.
// Revived by Thongchai Kolyutsakul (21 May 2015).
//
// USAGE: UIColor *mycolor = [UIColor pxColorWithHexValue:@"#BADA55"];
// UIColor *mycolor = [UIColor pxColorWithHexValue:@"FFFFFF"];
// UIColor *mycolor = [UIColor pxColorWithHexValue:@"1AD"];
@hlung
hlung / LowestCommonAncestor.swift
Last active Oct 25, 2020
Find the least common ancestor of 2 views.
View LowestCommonAncestor.swift
import Foundation
import UIKit
// From an example view hierarchy...
// 1 -> 2 -> 3 -> 4 -> 5
// |
// ---> 6
// Find the least common ancestor of 2 views.
// Input