6

I have an NSTextView that I need to insert a line break in. The code looks like this:

NSString *myString = @"test"
[myTextView insertText:myString];

/**** INSERT LINE BREAK HERE ****/

[[myTextView textStorage] insertAttributedString:MY_ATTRIBUTED_STRING atIndex:myString.length];

Anybody have any idea how to do this?

Bob
  • 61
  • 1
  • 2

3 Answers3

5

I'm way late to the game here, but there's something that needs mentioning:

NSTextView is a subclass of NSResponder. NSResponder declares but does not implement a method

-insertNewline:

NSTextView does implement that method. That's the way to do it, not manually building a string with '\n'.

Dexter
  • 5,666
  • 6
  • 33
  • 45
5

I believe you can use the literal \n for a newline

NSString *myString = @"test\n"
[myTextView insertText:myString];
Jason
  • 86,222
  • 15
  • 131
  • 146
1

I agree with Jason. Use \n. I KNOW it works for iPhone, so it should definitely work for Mac.

Also, you forgot a semicolon on the end of the first line ;)

Glenn Smith
  • 912
  • 1
  • 17
  • 37