0

I'd like to make some line breaks in an UITextView i saw in different topics here that @"\n" should work...

if i try :

textView.text = @"Hey \nDude !";

that's work fine ! But if my text come from a plist file (with a root type array)

textView.text = [NSString stringWithString:[timeCodeArray objectAtIndex:textNumber]];

the text in the plist is :

<string>Hey \nDude !</string>

the \n is written in the text and there is no new line breach...

did a make something wrong ?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
user1081147
  • 1
  • 1
  • 1

2 Answers2

11
NSString *aStr = [timeCodeArray objectAtIndex:textNumber];
textView.text = [aStr stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];

I don't know why.

fannheyward
  • 18,599
  • 12
  • 71
  • 109
0

The reason is that when you save a string into the textView.text, it will try to render it as it is seen in the string.

This means it tries to show the "\n", instead of a new line.

To do this, (from regular expression) an extra '\' is used, to mask the functionality of \n just to show "\n". Internally it is stored as "\n" in the string.

string = @"hey \nDude !";
textView.text = [string stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
Luffy
  • 401
  • 5
  • 10