How to make UIButton's text alignment center? Using IB
Solution 1
Solution1
You can set the key path in the storyboard
Set the text to your multiline title e.g. hello
⌥ + ↵ multiline
You need to press ⌥ + ↵ to move text to next line.
Then add the key path
titleLabel.textAlignment
as Number
and value 1
, 1
means NSTextAlignmentCenter
titleLabel.numberOfLines
as Number
and value 0
, 0
means any number of lines
This will not be reflected on IB/Xcode, but will be in centre at run time (device/simulator)
If you want to see the changes on Xcode you need to do the following: (remember you can skip these steps)
-
Subclass the UIButton to make the button designable:
import UIKit @IBDesignable class UIDesignableButton: UIButton {}
Assign this designable subclass to the buttons you're modifying:
- Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):
Solution2
If you want to write the code, then do the long process
1.Create IBOutlet
for button
2.Write code in viewDidLoad
btn.titleLabel.textAlignment = .Center
btn.titleLabel.numberOfLines = 0
Solution3
In newer version of xcode (mine is xcode 6.1) we have property attributed title
Select Attributed
then select the text and press centre option below
P.S. The text was not coming multiline for that I have to set the
btn.titleLabel.numberOfLines = 0
Solution 2
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
Solution 3
Use the line:
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
This should center the content (horizontally).
And if you want to set the text inside the label to the center as well, use:
[labelOne setTextAlignment:UITextAlignmentCenter];
If you want to use IB, I've got a small example here which is linked in XCode 4 but should provide enough detail (also mind, on top of that properties screen it shows the property tab. You can find the same tabs in XCode 3.x):
Solution 4
For UIButton you should use:-
[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
Solution 5
For ios 8 and Swift
btn.titleLabel.textAlignment = NSTextAlignment.Center
or
btn.titleLabel.textAlignment = .Center
Related videos on Youtube
Inder Kumar Rathore
Develops mobile applications (primarily iOS) Open source work https://github.com/InderKumarRathore LinkedIn profile https://in.linkedin.com/in/InderKumarRathore Personal Site: http://inderkumar.herokuapp.com 📧rathore619🌀gmail🔘com But before emailing please read Jon Skeet's blog post on Stack Overflow-related emails first. Blogs Property in Objective - C
Updated on October 10, 2021Comments
-
Inder Kumar Rathore over 2 years
I can't set the title of UIButton using IB as center. My title is multi line.
It is giving like this one
But I want like this one
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
-
Sagar Kothari about 11 yearsKumar Rathor - try by code
self.yourButton.titleLabel.textAlignment = UITextAlignmentCenter;
-
-
Inder Kumar Rathore about 13 yearsthanks...but I don't want to set it through IB. :( that's my last option of doing that...
-
Joetjah about 13 years@Inder Kumar Rathore: No problem. To set it in IB, you should click on the label, then in the Attributes Inspector, set Alignment to "Center". In my IB (XCode 4), it has 3 icons showing the alignment of text. Obviously, you should pick the middle one then.
-
Inder Kumar Rathore about 13 yearscan you please send me the screen shot... Is this the label of the Button or you have draged the UILabel from the library??? I suppose it UILabel not button's label
-
Joetjah about 13 years@Inder Kumar Rathore: Hey, check out the screenshot I added. I have indeed mistaken the kind of labels you use, I was still thinking on UILabel instead of the text in UIButton. The screenshot shows how to align on the UIButton through IB.
-
Inder Kumar Rathore almost 13 yearsYeah I have done it but it just align the button's content.... :( try multi line title you will find that...
-
OC Rickard over 10 yearsUITextAlignmentCenter has been deprecated. Use NSTextAlignmentCenter now.
-
Joetjah over 10 years@OCRickard Feel free to add explanation, I'm not actively working with Objective-C or iOS development anymore.
-
OC Rickard over 10 yearsWouldn't let me edit, but NSTextAlignmentCenter has taken the place of UITextAlignmentCenter. They mean exactly the same thing tho. developer.apple.com/library/iOS/documentation/UIKit/Reference/…
-
Sullivan over 9 yearsIt has became NSTextAlignment in later versions of iOS.
-
Alberto Mier over 7 yearsthas code not work, the best answer is @joetjah
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
-
SwiftArchitect over 6 yearsThe question explicitly mentions IB, as in Interface Builder. This is not the correct answer, as it requires coding.
-
Lewis Black about 6 yearsSwift 4: myButton.titleLabel?.textAlignment = .center
-
ModusPwnens almost 6 years'Center' has been renamed to 'center'
-
finx almost 5 yearsFor solution 3, you can just add
Line Breaking = Clip
, it will transform the text to multiline -
Владимир Ковальчук over 4 yearsuse one "?" in sender.titleLabel? instead "??"
-
Brian Hong about 4 yearsSolution 3 has problem, at least with Xcode 11.3.1: when I change button's title, it doesn't work. when I used
btn.titleLabel.textAlignment
, it works great.