Thursday, March 17, 2011

iOS Linebreak Fun

Learned something the hard way today.  You'd think that the order in which properties are set shouldn't matter even in weirdo Objective-C land, but things are different over there.

The following will wrap long button text, but limit it to two lines.

button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button.titleLabel.numberOfLines = 2;

The following will wrap long button text all over your image and outside the bounds of your button.  You'll be scratching your head for a while wondering why the numberOfLines property doesn't have any effect.

button.
titleLabel.numberOfLines = 2;
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;