我想在名为“粘贴”的按钮上将所选文本粘贴到文本视图中.我该怎么做



我是可可印刷的新手,我正在使用代码复制文本:

NSRange range = [textView selectedRange];
NSData* rtfData = [textView RTFFromRange: range];
NSAttributedString* aStr = [[NSAttributedString alloc]initWithRTF:rtfData documentAttributes:NULL];
NSString* str = [aStr string];

str包含所选文本,但是每当我在文本视图中单击时,如何将该文本粘贴到NSTextView中?

您必须将

NSTextView的委托设置为self,然后您需要实现要执行此操作的委托方法。

然后你可以使用NSText的setString方法(NSTextView的超级类)来设置文本。

编辑 -(如果将文本设置为文本视图,请先尝试使用此功能)

NSRange range = [textView selectedRange]; 
NSData* rtfData = [textView RTFFromRange: range];
NSAttributedString* aStr = [[NSAttributedString alloc]initWithRTF:rtfData documentAttributes:NULL];
NSString* str = [aStr string];
[[textView setString:str];

对于NSTextView,您需要:

[[textView textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:str]]; 

相关内容

最新更新