54

I have id textInput and I insert new line(\n) OK with:

[textInput insertText:@"\n"];

But when input Text from label.text (Input in Interface Builder) ,it NOT OK. Just input \n text.

NSLog(@"%@",label.text);
[textInput insertText:label.text];

How to input special character when store it in label.text?

I don't want to compare [inputStr isEqualToString:@"\\n"];

*Log: \n

Thanks!

Alex
  • 1,161
  • 1
  • 8
  • 15
  • 3
    They are the same. You're doing something wrong. – Hot Licks Mar 16 '13 at 02:43
  • I put \n to label and [textInput insertText:label.text], NOT Work, just get \n text. Nothing wrong here. – Alex Mar 16 '13 at 02:47
  • Hint: If you NSLog the data and it prints "\n" then it doesn't contain "\n". – Hot Licks Mar 16 '13 at 02:53
  • 1
    They are the same. You're doing something wrong. (And you're not showing us your real code.) – Hot Licks Mar 16 '13 at 03:00
  • I've edit, please check! Something wrong when input text from UILabel.text. Thanks! – Alex Mar 16 '13 at 03:02
  • 2
    Your problem is that label.text doesn't contain the newline code represented by "\n" but instead contains the characters "\" and "n". This could presumably happen if you put "\n" into the text field of a label in Interface Builder, but if you code the C string "\n" or the Objective-C NSString @"\n" that combo will always be converted to newline. – Hot Licks Mar 16 '13 at 03:07
  • You're right! So how to solve when input from IB? – Alex Mar 16 '13 at 03:09
  • Don't input your text with IB. No need to. Or, if you insist, translate the characters afterwards. replaceOccurrencesOfStringWithString, IIRC. – Hot Licks Mar 16 '13 at 03:15
  • That's depend on my App. Thanks for your time! – Alex Mar 16 '13 at 03:17

2 Answers2

169

Try option-return or pasting in the newline.

Matthew Knippen
  • 1,048
  • 9
  • 22
tc.
  • 33,468
  • 5
  • 78
  • 96
14

@Hot Licks explained the reason well in comment.

The only way is

if ([self.mylabel.text isEqualToString:@"\\n"]) {

   [self.myTextView insertText:@"\n"];
}

Manually, use ALT+ENTER

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102