Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton title text truncating

I have a UIButton that is set in the XIB as 147 pixels wide and 30 high. It have a default text of 'Navigation'. During the course of the application, I set the title to 'Global'. Later, I then set it t0 'Summary' and this display as 'Su...ry'.

I have read so many things that say to set the default label for a UIButton to be longer than the longest text, however 'Summary' is shorter than 'Navigation'. The text area seems to shrink during the application.

I do not reload the view. My application predominantly clears elements from a default display scrollview and refills with new data.

I have been using

button.titlelabel.text = @"Summary";

however I was getting occasions when this did not result in the text showing, so starting using

[button setTitle:@'Summary' forState:UIControlStateNormal];

Which worked every time.

Is there some way to force the text area within a UIButton to reset to the text being displayed? I would rather not create buttons on the fly, and if this is a function of objective-c, then I would consider this a fault.

Thanks in advance. Chris H

like image 303
Chris Hardaker Avatar asked Dec 22 '11 23:12

Chris Hardaker


1 Answers

It's not a fault of Objective-C. Objective-C is a programming language. If anything is at fault it's UIKit which is Apple's UI framework for iOS.

The problem is that your button won't resize - that's not how the API for UIButton has been designed. You can solve it by resizing the button yourself after setting the title. You can determine how big the text has to be with:

CGSize requiredSize = [[button titleForState:UIControlStateNormal] sizeWithFont:button.titleLabel.font];

Then you can use that to make it a bit bigger in width and height and set the frame of button to that.

like image 75
mattjgalloway Avatar answered Sep 23 '22 11:09

mattjgalloway