How to make UIButton's text alignment center? Using IB

Inder Kumar Rathore picture Inder Kumar Rathore · Apr 19, 2011 · Viewed 125.8k times · Source

I can't set the title of UIButton using IB as center. My title is multi line.
It is giving like this one
enter image description here

But I want like this one
enter image description here

I have given space in this but I don't want to do that. As it is not aligned exactly for some cases and I know there is a property of UILabel to set the alignment but I don't want to write a code for that.. just want to set everything from IB.

Thanks

Answer

RGML picture RGML · Sep 20, 2011

This will make exactly what you were expecting:

Objective-C:

 [myButton.titleLabel setTextAlignment:UITextAlignmentCenter];

For iOS 6 or higher it's

 [myButton.titleLabel setTextAlignment: NSTextAlignmentCenter];

as explained in tyler53's answer

Swift:

myButton.titleLabel?.textAlignment = NSTextAlignment.Center

Swift 4.x and above

myButton.titleLabel?.textAlignment = .center