Search code examples
iosuitableviewios5uibuttonautoresize

AutoResizing a button based on title text content


So I have a strange issue which could be a bug or me being stupid.

I have a button in a cell. The cell shows the logged in users facebook friends. If they are already a user of the app it shows the button as Play, else it shows Invite Me...

UIButton *playButton = (UIButton *)[cell.contentView viewWithTag:30];
playButton.backgroundColor = [UIColor clearColor];
[playButton setBackgroundImage:[[UIImage imageNamed:@"fbCell_Invite_30h"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 5, 15, 5)] forState:UIControlStateNormal];    
playButton.titleLabel.text = @"Invite me";
if ([self.fbFriendsUsingApp containsObject:[friend valueForKey:@"id"]]) {
    playButton.titleLabel.text = @"Play";
}

This all works fine and not an issue. However, when the button text is invite me it shows as this: enter image description here

In storyboard interface builder the button is setup like this: enter image description here

What is strange is that if I click the middle arrow <------> to make the width stretch, the title text stops working and they all show 'Play' ???


Solution

  • Try setting the text of the button using

    [playButton setTitle:@"Invite Me" forState:UIControlStateNormal];

    instead of

    playButton.titleLabel.text = @"Invite Me;

    I've found that if I don't use the first approach above the changes to the button text don't show up.