Skip to content

Instantly share code, notes, and snippets.

View Marcocanc's full-sized avatar
🚀

Marco Cancellieri Marcocanc

🚀
View GitHub Profile
@Marcocanc
Marcocanc / Label.swift
Last active September 3, 2023 09:14
A UILabel subclass that can hold attributes and apply them to text
import UIKit
/// A UILabel subclass that can hold attributes and apply them to text
@IBDesignable
final class Label: UILabel {
convenience init(attributes: [NSAttributedStringKey: Any]) {
self.init()
self.attributes = attributes
}
// MARK: Properties
@Marcocanc
Marcocanc / HomeKitQR.swift
Created January 3, 2018 09:38
Reverse engineering HomeKit QR Code decoding/encoding
import Foundation
func setupCodeFrom(uri: String) -> String {
let startIndex = uri.index(uri.startIndex, offsetBy: "X-HM://".count)
let endIndex = uri.index(uri.endIndex, offsetBy: -4)
var actualString = uri[startIndex..<endIndex]
let number = UInt(actualString, radix: 36)!
var code = String(format: "%08u", number & 0x7ffffff)
@Marcocanc
Marcocanc / UILabelCopyable.swift
Created December 19, 2017 09:33
UILabel subclass that supports Copying its contents to clipboard
import UIKit
class UILabelCopyable: UILabel {
override func copy(_ sender: Any?) {
UIPasteboard.general.string = text
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
@Marcocanc
Marcocanc / Array+filterKeyPath.swift
Created December 18, 2017 11:14
Filter with KeyPath
extension Array {
func filter<T: Equatable>(where keyPath: KeyPath<Element, T>, equals compareValue: T) -> [Element] {
return filter { $0[keyPath: keyPath] == compareValue }
}
}
@Marcocanc
Marcocanc / Array+sortedByKeyPath.swift
Last active December 18, 2017 11:06
Array sorting by keypath
extension Array {
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>, ordered areInIncreasingOrder: (T, T) -> Bool = (<) ) -> Array<Element> {
return self.sorted(by: {
areInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath])
})
}
}
@Marcocanc
Marcocanc / Sequence+groupBy.swift
Created December 12, 2017 09:47
Swift 4 Keypath for grouping sequences
import Foundation
internal extension Sequence {
func grouped<T>(by keyPath: KeyPath<Element, T>) -> [T: [Element]] {
var groups = [T: [Element]]()
for element in self {
let key = element[keyPath: keyPath]
if !groups.keys.contains(key) {
groups[key] = [Element]()
}
@Marcocanc
Marcocanc / ripe.sh
Created July 10, 2017 07:15
Domain to Ripe Lookup
#!/bin/bash
IP=$(dig +short $1 | sed -n 1p)
whois -h whois.ripe.net -- "-T route ${IP}"
@Marcocanc
Marcocanc / UIFont+font.swift
Created June 15, 2017 12:23
Custom Fonts in iOS
import UIKit
extension UIFont {
/// Returns the font in the specified size and weight. Default weight is `regular`
open class func font(ofSize fontSize: CGFloat, weight: FontWeight = .regular) -> UIFont {
let fontName = "UniversNextPro-\(weight.rawValue)"
guard let font = UIFont(name: fontName, size: fontSize) else {
fatalError("\(fontName) not found")
}
return font
@Marcocanc
Marcocanc / FontWeight.swift
Last active June 15, 2017 12:23
Custom Fonts in iOS
import Foundation
/// Custom FontWeights to be used with the .font UIFont class functions
public enum FontWeight: String {
case regular = "Regular"
case medium = "Medium"
case black = "Black"
case light = "Light"
case thin = "Thin"
case bold = "Bold"
@Marcocanc
Marcocanc / scw-rclone-sync.sh
Last active December 31, 2021 11:48
Create a scaleway instance, put rclone and its config on it, sync drives and then terminate the the server
#!/bin/bash
set -e
SCALEWAY='scw --region="ams1"'
ARCH=X64
COMM_TYPE=$ARCH-2GB
IMAGE=xenial
TRANSFERS=7
TG_TOKEN=<BOT_TOKEN>