A collection of useful Swift extensions and subclasses
PocketKnive is only available through Carthage. To install just add the following to your Cartfile:
github "uargh/PocketKnive"
let insetLabel = PKEdgeLabel()
insetLabel.backgroundColor = .red
insetLabel.text = "Hallo World"
insetLabel.bottomTextInset = 8
insetLabel.leftTextInset = 16
insetLabel.rightTextInset = 16
insetLabel.topTextInset = 8
// OR
insetLabel.textInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
Array.remove(_ element: Element) -> Index?
Array.remove(_ elements: [Element]) -> [Index]
var array: [Int] = Array(0...10)
let removedIndex = array.remove(5)
print("\(removedIndex)") // Optional(5)
print("\(array)") // [0, 1, 2, 3, 4, 6, 7, 8, 9, 10]
var array: [Int] = Array(0...10)
let removedIndices = array.remove([1, 3, 5, 7, 9])
print("\(removedIndices)") // [1, 2, 3, 4, 5]
print("\(array)") // [0, 2, 4, 6, 8, 10]
Date().weekOfYear // 1 - 52
let string = "Lorem ipsum dolor"
print(string.base64Encoded) // Optional("TG9yZW0gaXBzdW0gZG9sb3I=")
let string = "TG9yZW0gaXBzdW0gZG9sb3I="
print(string.base64Decoded) // Optional("Lorem ipsum dolor")
let string = "capitalized?"
print(string.capitalizingFirstLetter()) // "Capitalized?"
var string = "capitalized?"
string.capitalizeFirstLetter()
print(string) // "Capitalized?"
print("\("abcdefg".containEmoji)") // false
print("\("π".containEmoji)") // true
String.height(forWidth width: CGFloat, attributes: [NSAttributedString.Key : Any]?) -> CGFloat
"Lorem ipsum dolor sit amet, consectetur adipisicing elit.".height(forWidth: 200, attributes: nil) // 28.0
let copy = "Lorem ipsum dolor sit amet, consectetur adipisicing elit."
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 1.25
let attributes: [NSAttributedString.Key : Any] = [
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 32),
NSAttributedString.Key.kern : 0.25,
NSAttributedString.Key.paragraphStyle : paragraphStyle
]
copy.height(forWidth: 200, attributes: attributes) // 287.0
print("\("a@b.c".isValidEmail)") // true
print("\("a@b".isValidEmail)") // false
override func viewDidLoad() {
let gR = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(sender:)))
self.view.addGestureRecognizer(gR)
}
@objc func handleTap(sender: UITapGestureRecognizer) {
sender.removeFromView()
}
let label = UILabel()
label.rotation = 5
UIScrollView.snapshot -> UIImage?
let scrollView = UIScrollView()
guard let snapshot = scrollView.snapshot else { return }
let vc = ViewController()
self.presentOverCurrentContext(viewController: vc, animated: true, completion: nil)
Jonas Heitzer
Do whatever you want with it, i don't care.