I have the problem that the text on three UIButtons gets wrapped to several lines in the Interface Builder, but when I run the code, the text is larger than the button and in one line only. I tried setting NSLineBreakMode and NSTextAlignment, but both didn't help. In the Interface Builder it looks correctly like this: https://i.stack.imgur.com/uuayd.jpg while on the simulator it looks like this: https://i.stack.imgur.com/vgkut.jpg. Any ideas? Thanks in advance.
-
Have you tried to set uibutton's titleLabel.numberOfLines to 2 or 0? – kovpas Apr 22 '13 at 12:34
-
Yeah I had tried that before and it didn't help. Also, before trying to make autolayout work in landscape mode, it all worked well :( – N4zroth Apr 22 '13 at 12:38
-
possible duplicate of [can't get word wrap to work on UIButton](http://stackoverflow.com/questions/15428796/cant-get-word-wrap-to-work-on-uibutton) – matt Apr 23 '13 at 05:08
-
Nope, his answer didn't help me. That it works in IB and not in the real application is kinda weird to me, too. – N4zroth Apr 23 '13 at 13:29
1 Answers
There is certainly something very odd about your example; I can't reproduce it. You must be doing something to the buttons that you have not described in the information provided by your question. If you set up your button line break mode to be Word Wrap (in the nib), and if your constraints are sensible so that the button can get wider in landscape and narrower in portrait, then it will wrap in portrait and not in landscape, which I believe is what you want. Here are screen shots of a button in the Simulator on my machine (ignore the actual widths of the buttons; it's only an example; what's important is the text wrapping):
The real problem, however, is the height. You'll notice that that isn't changing. This is because a round rect button has an intrinsic height value. If you want the height to change, to make more vertical room for the wrapped text, you will probably need to subclass or intervene in the layout process after rotation. For example, I get pretty nice results like this:
-(void)viewDidLayoutSubviews {
CGRect f = self.button.bounds;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
f.size.height = 44;
else
f.size.height = 60;
self.button.bounds = f;
}

- 515,959
- 87
- 875
- 1,141
-
Thanks ... I know that it's odd but as I got to know the IB tends to behave oddly at times (disappearing navigation bar and so on). I had solved the problem by some lines of code: `- (void)viewDidLayoutSubviews { self.buttonOne.titleLabel.preferredMaxLayoutWidth = self.buttonOne.frame.size.width; self.buttonOne.titleLabel.numberOfLines = 0; self.buttonOne.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; self.buttonOne.titleLabel.textAlignment = NSTextAlignmentCenter; [self.view layoutSubviews]; }` However, if I added the button anew, my whole layout would just break :/ – N4zroth Apr 25 '13 at 14:33