Jules
Jules

Reputation: 7766

iPhone, how do I center button.titleLabel text?

I change the text of one of my buttons, I didn't realize that the text wasn't centered.

Its set in IB

How do I center it ?

Upvotes: 2

Views: 4691

Answers (4)

Jan ATAC
Jan ATAC

Reputation: 1262

In iOS 9 with Swift :

myButton.titleLabel.textAlignment = NSTextAlignment.Center

Upvotes: 0

Eric
Eric

Reputation: 4061

Now (iOS 6):

 myButton.titleLabel.textAlignment = NSTextAlignmentCenter;

change from UI to NS

Upvotes: 0

deanWombourne
deanWombourne

Reputation: 38475

It's just a text label so this should work (put it in your viewDidLoad method and set myButton as an IBOutlet property)

myButton.titleLabel.textAlignment = UITextAlignmentCenter;

However, it should already be centered - it seems odd that it isn't. Are you doing anything else unusual?

Upvotes: 6

Josh Hinman
Josh Hinman

Reputation: 6805

In Interface Builder, select the button and press ⌘-1 to bring up the attributes inspector. Look towards the bottom of the attributes, under "Control". You will see something that says "Content" with alignment buttons. Press the button for center alignment.

Upvotes: -1

Related Questions