Accelerating iOS Development with extensions

There are lot of ways you can accelerate your iOS development just by simple extensions to your project.

So, today, I am going to demonstrate some of the basic UIKit extensions in order to accelerate your iOS Development.

Elements Simplified

Just imagine, you are making for form for your new app. There are lot of elements that you are going to need again and again during the development, like UIButtons, UITextfields and UILabel.

Either you use storyboard or complete code, you have to write some code. That’s mandatory.

So, you can write custom functions that can simplify the common properties of the these elements just by one function call.

Here comes Protocol oriented programming to rescue you. And extensions have just entered the room.

Less talking, more coding! Let’s go for it.

Some front end extension demonstration

Quick Tip: I encourage everyone to write a documentation of whatever you code. It is a very good practice. 

extension UILabel {
   /**
   Moditfy your label in just one function call by adding some of the questions answers
     
     - Parameter text : Text you want to add
     - Parameter fontSize : The font size of the label
     - Parameter color : The color of the label
     - Parameter weight : The weight of the label (optional)
     - Parameter alignment : Adds the alignment to the label (optional, the default is left)

 */
    public func LabelSimplified(text: String, fontSize: CGFloat,color: UIColor,weight: UIFont.Weight? , alignment: NSTextAlignment?){
        
        self.text = text
        self.font = UIFont.systemFont(ofSize: fontSize, weight: weight ?? UIFont.Weight.regular)
        self.textColor = color
        self.textAlignment = alignment ?? .left
        self.numberOfLines = -1
    }
    
}

The above code can strike some spark regarding how you can accelerate your app development like this. Here open a room of endless opportunities of what you can with extensions.

so the next time you want to edit your label programatically, you just have to do one function call

@IBOutlet weak var label: UILabel! 

override func viewDidLoad(){

//MARK: Editing Labels 
label.LabelSimplified(text: "Hello World", fontSize: 25, color: black, weight: .semibold, alignment: .center)

}

Just few extensions can save you a lot of time and code. Here is one more example of the UIKit elements simplified:

extension UITextField{
  func TextFieldSimplified(_ placeholder: String, contentMode: UITextContentType,size: CGFloat, isPassword: Bool){
        self.placeholder = placeholder ?? ""
        self.textContentType = contentMode
        self.font = UIFont.systemFont(ofSize: size) 
        self.isSecureTextEntry = isPassword
       
    }
}

Where’s the next?

From here, your next step is to apply this type of protocol-oriented programming in your next iOS Project.

For more, check out this Github repository some more ideas.

Useful-UIKit-Extension-Swift

Check out couple other articles here

Avoiding Spaggati code

Thanks for reading the article! Hope you have a codeful day!

Leave a comment