简体   繁体   English

如何让Cocoa NSTextView随着用户输入而增长?

[英]How do I make a Cocoa NSTextView grow as the user types into it?

For a Cocoa application I am writing, I would like to support a panel to the right of the main document content where users can add notes for the currently selected document content. 对于我正在编写的Cocoa应用程序,我想支持主文档内容右侧的面板,用户可以在其中添加当前所选文档内容的注释。 (If you are familiar with Microsoft Word or Scrivener, this feature is similar to the comment feature in those applications.) Scrivener does a nice job of starting with a text field sized to fit the default text, and then growing it taller as the user types into it. (如果您熟悉Microsoft Word或Scrivener,此功能类似于这些应用程序中的注释功能。)Scrivener可以很好地开始使用大小适合默认文本的文本字段,然后随着用户增长更高键入它。 I'd like to implement the same behavior for my Cocoa app. 我想为我的Cocoa应用程序实现相同的行为。

What's the basic strategy? 什么是基本策略?

There are delegate methods that allow you to capture the actual keystrokes as they come in. 有一些委托方法可以让您捕获实际的击键。

Implement the below delegate method to resign first responder, based upon the keyboard 实现以下委托方法以根据键盘重新签署第一响应者

-(BOOL)textFieldShouldReturn:(UITextField *)textfield

Implement the below delegate method to detect when focus has been given back to the TextField. 实现以下委托方法以检测何时将焦点返回给TextField。 You may also want to perform the deletion of current text, or retain the text that was already there if you wish 您可能还希望删除当前文本,或者如果您愿意,还可以保留已存在的文本

-(void)textFieldDidBeginEditing:(UITextField *)textfield

Implement the below delegate method to detect the character(s) entered and where (based on the caret position), and essentially add the characters to your privately held and displayed string (displayed out to your textfield that is) 实现下面的委托方法来检测输入的字符和位置(基于插入位置),并基本上将字符添加到您的私有和显示的字符串(显示到您的文本字段)

-(BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString

Implement the below delegate method to detect when editing has finished so that you can perform any other cleanup etc... that you wish to do. 实现以下委托方法以检测编辑何时完成,以便您可以执行任何其他清理等...您希望这样做。

-(void)textFieldDidEndEditing:(UITextField *)textField

I will get back to you on the dynamic sizing of your TextView itself, but that (at least on iOS) as Ive seen has a solution and at one point I have used it. 我将回复你关于TextView本身的动态调整大小,但是(至少在iOS上)我已经看到了解决方案,并且我曾经使用过它。 You will essentially make your font size static, potentially your width static, then edit your height based on how many lines you have, or you could keep your height static, and change your width based on characters, etc... up to you. 基本上你的字体大小是静态的,可能你的宽度是静态的,然后根据你有多少行来编辑你的高度,或者你可以保持你的高度静态,并根据字符等改变你的宽度......由你决定。

Here is a great set of responses on StackOverflow about dynamic sizing How do I size a UITextView to its content? 这是关于动态大小调整的StackOverflow的一组很好的响应如何将UITextView的大小调整为其内容?

So if you combine the keystroke recognition with the dynamic sizing you should have it!!! 因此,如果您将击键识别与动态尺寸相结合,您应该拥有它!

A custom non NSScrollView embedded NSTextView does the trick. 自定义非NSScrollView嵌入式NSTextView可以解决问题。 See my answer here How do I make NSTextView grow with text? 在这里查看我的答案如何使NSTextView与文本一起成长? .

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

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