简体   繁体   English

NSTextView插入换行符

[英]NSTextView Insert Line Break

I have an NSTextView that I need to insert a line break in. The code looks like this: 我有一个NSTextView,我需要插入一个换行符。代码如下所示:

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? 任何人都知道如何做到这一点?

I'm way late to the game here, but there's something that needs mentioning: 我在这里比赛迟到了,但有些东西需要提及:

NSTextView is a subclass of NSResponder. NSTextView是NSResponder的子类。 NSResponder declares but does not implement a method NSResponder声明但未实现方法

-insertNewline:

NSTextView does implement that method. NSTextView 确实实现了该方法。 That's the way to do it, not manually building a string with '\\n'. 这是做到这一点的方法,而不是用'\\ n'手动构建字符串。

I believe you can use the literal \\n for a newline 我相信你可以使用文字\\ n作为换行符

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

I agree with Jason. 我同意杰森的观点。 Use \\n. 使用\\ n。 I KNOW it works for iPhone, so it should definitely work for Mac. 我知道它适用于iPhone,因此它绝对适用于Mac。

Also, you forgot a semicolon on the end of the first line ;) 另外,你在第一行的末尾忘了一个分号;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM