Skip to content

A collection of useful Swift extensions and subclasses

License

Notifications You must be signed in to change notification settings

uargh/PocketKnive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PocketKnive

A collection of useful Swift extensions and subclasses

Carthage compatible Swift 4.2

Install

PocketKnive is only available through Carthage. To install just add the following to your Cartfile:

github "uargh/PocketKnive"

Subclasses

PKEdgeLabel

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)

Extensions

Array

remove

Array.remove(_ element: Element) -> Index?
Array.remove(_ elements: [Element]) -> [Index]

Remove single element

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]

Remove multiple elements

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

Date().weekOfYear // 1 - 52

String

base64

base64Encoded

let string = "Lorem ipsum dolor"
print(string.base64Encoded) // Optional("TG9yZW0gaXBzdW0gZG9sb3I=")

base64Decoded

let string = "TG9yZW0gaXBzdW0gZG9sb3I="
print(string.base64Decoded) // Optional("Lorem ipsum dolor")

capitalizingFirstLetter

let string = "capitalized?"
print(string.capitalizingFirstLetter()) // "Capitalized?"

capitalizeFirstLetter

var string = "capitalized?"
string.capitalizeFirstLetter()
print(string) // "Capitalized?"

containEmoji

print("\("abcdefg".containEmoji)") // false

print("\("πŸ˜‹".containEmoji)") // true

height

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

isValidEmail

print("\("a@b.c".isValidEmail)") // true

print("\("a@b".isValidEmail)") // false

UIGestureRecognizer

removeFromView

override func viewDidLoad() {
    let gR = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(sender:)))
    self.view.addGestureRecognizer(gR)
}

@objc func handleTap(sender: UITapGestureRecognizer) {
    sender.removeFromView()
}

UILabel

rotation

let label = UILabel()
label.rotation = 5

UIScrollView

snapshot

UIScrollView.snapshot -> UIImage?
let scrollView = UIScrollView()
guard let snapshot = scrollView.snapshot else { return }

UIViewController

presentOverCurrentContext

let vc = ViewController()
self.presentOverCurrentContext(viewController: vc, animated: true, completion: nil)

Author

Jonas Heitzer

License

Do whatever you want with it, i don't care.

About

A collection of useful Swift extensions and subclasses

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published