Domanda

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' ???

È stato utile?

Soluzione

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.

Altri suggerimenti

You can find the size of the NSString that you are setting as the button label and then resize the button accordingly:

CGSize labelSize = [labelString sizeWithFont:[UIFont systemFontOfSize:12]]; 
[button setFrame:CGRectMake(10,0,labelSize.width, labelSize.height)];

You will probably want to add a little to the CGSize as padding, but play with it and find the result you desire.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top