35

I've created a wide UIButton with Interface Builder (Xcode 4), added in a dummy 5 character title (e.g. Click) and then changed the title text programmatically later.

Odd thing is the width of the title text seems to remain the same so, if I use a longer piece of text (e.g. "Now click here"), it appears like this: "N...e"

Any idea what's going on?

UPDATE: If I use a long line of text in IB it's centred. However, once I've programmatically-changed this text it appears left-aligned!

vfn
  • 6,026
  • 2
  • 34
  • 45
Snowcrash
  • 80,579
  • 89
  • 266
  • 376

1 Answers1

109

You need to use the UIButton method setTitle:forState:

[self.myButton setTitle:@"Correct New Title" forState:UIControlStateNormal];

as this will correctly update the size and position of the buttons label. Where as just setting the title with self.myButton.titleLabel.text = @"Wrong New Title"; will not.

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
James Beith
  • 5,751
  • 3
  • 27
  • 21