Search code examples
swiftstringios9

Swift: Getting a string width to make a frame (using CGRect) in swift 2.2


How can I get width of a string in swift 2.2 and iOS 9 ?
What I want to do is to create a UIButton programmatically but its frame needs to fit the title size which will be inferred at runtime.
Note: We know frame's height.


Solution

  • After you have set the text of button just call :-

    yourButton.sizeToFit()