user1445205
user1445205

Reputation:

Shrink title label text of UIButton when it reaches max number of lines

I have a UIButton that I want resizing the text font depending on the size of the NSString. I want the maximum number of lines to be 3 and if the NSString length exceeds 3 lines then the font shrinks until all the text is shown. This is what I have so far:

button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;

button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.numberOfLines = 3;

However this code does not do as I please; the UIButton text contains ... if the NSString exceeds 3 lines. Any ideas?

Upvotes: 0

Views: 1211

Answers (2)

Andy Obusek
Andy Obusek

Reputation: 12832

It is a bug to set the lineBreakMode when you want to the text to shrink. Just remove the line that sets lineBreakMode and it will work.

Upvotes: 2

Travis
Travis

Reputation: 3369

You also need to set the minimumScaleFactor of the label. The default is 0.

Upvotes: 0

Related Questions