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

132,198

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.

enter image description here

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

enter image description here

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)

  1. Subclass the UIButton to make the button designable:

    import UIKit
    @IBDesignable class UIDesignableButton: UIButton {}
    
  2. Assign this designable subclass to the buttons you're modifying:

Showing how to change the class of the button using Interface Builder

  1. Iff done right, you will see the visual update in IB when the Designables state is "Up to date" (which can take several seconds):

Comparing the designable and default button in Interface Builder



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

enter image description here

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): enter image description here

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

Share:
132,198

Related videos on Youtube

Inder Kumar Rathore
Author by

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, 2021

Comments

  • Inder Kumar Rathore
    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
    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

    • Sagar Kothari
      Sagar Kothari about 11 years
      Kumar Rathor - try by code self.yourButton.titleLabel.textAlignment = UITextAlignmentCenter;
  • Inder Kumar Rathore
    Inder Kumar Rathore about 13 years
    thanks...but I don't want to set it through IB. :( that's my last option of doing that...
  • Joetjah
    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
    Inder Kumar Rathore about 13 years
    can 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
    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
    Inder Kumar Rathore almost 13 years
    Yeah I have done it but it just align the button's content.... :( try multi line title you will find that...
  • OC Rickard
    OC Rickard over 10 years
    UITextAlignmentCenter has been deprecated. Use NSTextAlignmentCenter now.
  • Joetjah
    Joetjah over 10 years
    @OCRickard Feel free to add explanation, I'm not actively working with Objective-C or iOS development anymore.
  • OC Rickard
    OC Rickard over 10 years
    Wouldn'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/Referenc‌​e/…
  • Sullivan
    Sullivan over 9 years
    It has became NSTextAlignment in later versions of iOS.
  • Alberto Mier
    Alberto Mier over 7 years
    thas code not work, the best answer is @joetjah myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  • SwiftArchitect
    SwiftArchitect over 6 years
    The question explicitly mentions IB, as in Interface Builder. This is not the correct answer, as it requires coding.
  • Lewis Black
    Lewis Black about 6 years
    Swift 4: myButton.titleLabel?.textAlignment = .center
  • ModusPwnens
    ModusPwnens almost 6 years
    'Center' has been renamed to 'center'
  • finx
    finx almost 5 years
    For solution 3, you can just add Line Breaking = Clip, it will transform the text to multiline
  • Владимир Ковальчук
    Владимир Ковальчук over 4 years
    use one "?" in sender.titleLabel? instead "??"
  • Brian Hong
    Brian Hong about 4 years
    Solution 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.

Related