Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton label text is being clipped

I have a UIButton built in Interface Builder that has a default label. In Xcode, I'm changing the label text dynamically like so:

myButton.titleLabel.text = @"this is the new label"; 

However, when the text updates, the new string is being clipped down to the same size as the original string and ends up looking like:

this...label 

Anyone know why this is happening?

like image 678
thenameisnick Avatar asked Jan 01 '11 23:01

thenameisnick


1 Answers

You should use setTitle:forState: to change the title of a UIButton. If you change the title yourself, the button has no indication that it needs to resize the label – you'd end up having to do something like this:

myButton.titleLabel.text = @"this is the new label"; [myButton setNeedsLayout]; 

but I'm not even sure that would work in all cases. Methods like setTitle:forState: are provided so that you can provide titles for multiple states without having to update the button manually, and so that the button knows that it needs to be laid out with a new title.

like image 143
Justin Spahr-Summers Avatar answered Oct 06 '22 07:10

Justin Spahr-Summers