In this tutorial, we will learn how to alter UIButton size as per button title text length. UIButton does not increase its size, when we change tittle of UIButton dynamically or programmatically in swift. When we create a UIButton in storyboard, and set a large title to it in our ViewController class. Then the size of UIButton will remain same as in the story board and current button title will be showed dotted from the middle. In this tutorial, we will resolve dotted UIButton title issue by overriding the interensicContent size of UIButton.
For this first we will create a custom class that inherits UIButton. There we will override the interinsicContentSize method of the UIButton. Below is the code snippets of the custom class. We named our custom class as “AdaptableSizeButton“.
Code to change UIButton size according to text
Since we have code for our custom class, in your storyboard change UIButton class to AdaptableSizeButton and see the results. If you have concerns do post a comment for same.