5+1 Swift extensions to make your life easier

Balázs Vincze
3 min readJan 25, 2020

Thanks for stopping by, today I’ll show you five small Swift extensions I use in almost every iOS project.

🏁 Here we go:

1. Padding a UIButton

I often find myself having to add some padding to a UIButton. Instead of tweaking around with the titleEdgesInsets and contentEdgeInsets, I created this simple CGSize extension, which I then use in UIButton subclasses:

2. Highlighting parts of a String

It’s common to implement some sort of partial string highlighting when adding autocomplete to an app. To save myself some time, I use the following extension. The example demonstrates usage with a UILabel.

3. Subscripting Array using an IndexPath

Have you ever written something like this in your tableView(_:cellForRowAt:) implementation?

Binding data to your cells using the row’s index path as the array subscript is common practice. I almost write this line of code (or something very similar) everyday, and sometimes I still forget to append .row to the index path. To save myself 0.15 seconds each time, I extended the default Swift Array with this simple function:

--

--