Skip to content

Instantly share code, notes, and snippets.

@hamdan
hamdan / MultipleTapLabel.swift
Last active November 15, 2023 21:26
Create Multiple Tappable Links in a UILabel
extension UITapGestureRecognizer {
func didTapAttributedTextInLabel(label: UILabel, targetText: String) -> Bool {
guard let attributedString = label.attributedText, let lblText = label.text else { return false }
let targetRange = (lblText as NSString).range(of: targetText)
//IMPORTANT label correct font for NSTextStorage needed
let mutableAttribString = NSMutableAttributedString(attributedString: attributedString)
mutableAttribString.addAttributes(
[NSAttributedString.Key.font: label.font ?? UIFont.smallSystemFontSize],
range: NSRange(location: 0, length: attributedString.length)
@hamdan
hamdan / decodable.swift
Created April 27, 2019 18:41
Extensions giving Swift's decodable API type inference super powers
// Private supporting types
private struct AnyCodingKey: CodingKey {
var stringValue: String
var intValue: Int?
init(_ string: String) {
stringValue = string
}
init?(stringValue: String) {
@hamdan
hamdan / Rotate.swift
Created March 13, 2020 12:10
Rotate Only One ViewController to Landscape Orientation
// 1. First of all, you need to make sure “Device Orientation” only on ‘Portrait’ mode.
//2. Add these two functions to AppDelegate, which helps you deal with embed in nav bar/tab bar situation mentioned above:
protocol Rotatable: AnyObject {
func resetToPortrait()
}
extension Rotatable where Self: UIViewController {
func resetToPortrait() {
@hamdan
hamdan / button.swift
Last active September 20, 2023 07:17
Single Select Button Groups using rxswift
private let disposeBag = DisposeBag()
let selectedButton = Observable.from(
filterButtons.map { button in
button.rx.tap.map { button }
}
).merge()
filterButtons.forEach { button in
selectedButton.map { $0 == button }