Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView Insert Line Break

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?

like image 678
Bob Avatar asked Jul 06 '10 01:07

Bob


3 Answers

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

NSString *myString = @"test\n"
[myTextView insertText:myString];
like image 53
Jason Avatar answered Oct 03 '22 05:10

Jason


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'.

like image 24
DexterW Avatar answered Oct 03 '22 03:10

DexterW


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 ;)

like image 31
Glenn Smith Avatar answered Oct 03 '22 04:10

Glenn Smith