6

I'm trying to set up a three-line UILabel using XCode 4.2 and Interface Builder, building for iOS 5. After placing the UILabel, I've set the number of lines to 3 (this problem also occurs with number of lines set to 0), and I've used option-Return to break the lines properly in the text property. This all appears correctly in the storyboard preview in Interface Builder. The problem is when I build and run the app in the simulator, the line breaks are entirely ignored and the text just wraps in the UILabel's view wherever it needs to as though there were no line breaks in the text.

The obvious quick solution is to just set the text for the UILabel in code, or make three UILabels. No problem there. I'm just vexed by why this is happening when the label is set up purely in IB. Anyone encounter the same issue?

Chris
  • 449
  • 5
  • 7
  • What happens if you copy and paste your text into the label in IB? i.e. write it out in TextEdit.app or something and then copy it in. – mattjgalloway Dec 05 '11 at 18:08
  • That's very odd because I do the exact same thing and it works for me, although I've not tried with storyboarding, just normal XIBs. And you say if you set it in code it works fine? – mattjgalloway Dec 05 '11 at 18:28
  • 1
    Yes, it works great if I just code it, as in: `label.text = @"first line\nSecond line\nThird line"` I'm wondering if it's a bug in this new storyboard thing. I haven't tried it in a nib. It's weird because the IB preview displays it correctly, it just doesn't work in the sim. I also haven't tried it on the actual device - may be a problem with the sim. – Chris Dec 05 '11 at 18:55
  • Have you had a look in the raw XML? Are there new lines in the string there? – mattjgalloway Dec 05 '11 at 19:23
  • Interesting idea. The XML had actual line breaks, not \n's. I tried replacing the text string in the XML with a string composed of \n's, but that just replaced the text property in IB with a string that was composed like: "First\nSecond\nThird", and the IB preview and the build product in the sim shows it this way. I've also now tried building and running on device, and it does the same thing: IB text property with option-Returns displays correctly, the build on device and sim ignores the line breaks. – Chris Dec 05 '11 at 20:13
  • I'd expect the XML to show proper line breaks rather than `\n` though unfortunately. I suggest trying with a normal NIB and then if that works try getting a sample project which shows it working with a NIB and not with a storyboard and file a radar. – mattjgalloway Dec 06 '11 at 09:19

2 Answers2

9

This should probably be opened as a bug, however here is a workaround:

Instead of option-Return, use control-Return AND hit it twice (leaving a blank line between the rows of text in the storyboard). It will then display properly both in the storyboard, and on the device.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
3

By using this property of label,

label.lineBreakMode=UILineBreakModeCharacterWrap;
label.numberOfLines=0;

We can add a a bunch of text. See below screenshot.

enter image description here

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • 1
    Please consider adding a short explanation to the code here. the code by itself is unclear. – Amir Jul 04 '12 at 10:07
  • It is a short explantion, In a label (blue color) I just given a paragraph to understand line breaker. – Vineesh TP Jan 02 '13 at 16:49