3

I have a simple UIButton and try to get word wrap but it always shows the text in one line exceeding the button size.

 NSString * text = NSLocalizedString(@"Start Loading",@"Start Loading");
_continueBtn.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
_continueBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
[_continueBtn setTitle:text forState:UIControlStateNormal];

Also in UI builder when I set word wrap there the text wraps - only when I run the app the text appears in one line.

What have I missed here?

user387184
  • 10,953
  • 12
  • 77
  • 147
  • I've updated my answer - I've tried the same code, that you've post, and it's working good. On what version of iOS you're running this code? – derpoliuk Mar 16 '13 at 09:54

1 Answers1

5

Try:

_continueBtn.titleLabel.numberOfLines = 0;

I've tried next code:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 70, 50);
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button setTitle:@"start loading"
        forState:UIControlStateNormal];
[self.view addSubview:button];

It's working good for me:

screenshot

derpoliuk
  • 1,756
  • 2
  • 27
  • 43
  • Commenting here, because I have not enough reputation to comment the question. @user387184 What about creating label and adding it to button? That should work. – derpoliuk Mar 15 '13 at 10:09
  • Yes - but this is really not a preferred solution :-) – user387184 Mar 15 '13 at 10:39
  • 1
    please don't ask me why - but suddenly it worked - maybe because I did a clean and deleted derived data ???? – user387184 Mar 16 '13 at 12:46
  • From now on I gonna delete derived data before posting questions to the stackoverflow :) Actually, I'm trying to clean projects when I'm changing resources and getting some strange errors. But I've never thought that cleaning derived data can fix them too. – derpoliuk Mar 17 '13 at 01:21