我如何使Cocoa NSTextView成长为用户键入它? [英] How do I make a Cocoa NSTextView grow as the user types into it?

查看:200
本文介绍了我如何使Cocoa NSTextView成长为用户键入它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我写的Cocoa应用程序,我想在主文档内容的右侧支持一个面板,用户可以为当前选择的文档内容添加注释。 (如果你熟悉Microsoft Word或Scrivener,这个功能类似于那些应用程序中的注释功能。)Scrivener做一个不错的工作,从一个文本字段开始大小适合默认文本,然后增长它高为用户类型。



基本策略是什么?



实现下面的代理方法来退出第一个响应者,基于键盘

   - (BOOL)textFieldShouldReturn:(UITextField *)textfield 

实现以下委托方法来检测焦点何时返回到TextField。如果您希望

   - (void),您可能还需要删除当前文本或保留已经存在的文本)textFieldDidBeginEditing:(UITextField *)textfield 

实现以下委托方法来检测输入的字符

 

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

实现下面的代理方法来检测编辑完成后,您可以执行任何其他清理等...你想做。

   - (void)textFieldDidEndEditing:(UITextField *)textField 

我将回到你的TextView本身的动态大小,但是(至少在iOS上)如我所见的有一个解决方案,我已经使用它。你将基本上使你的字体大小静态,可能你的宽度静态,然后根据你有多少行编辑你的高度,或者你可以保持你的高度静态,并改变你的宽度基于字符等。



下面是关于StackOverflow关于动态大小的一组很好的响应:


因此,如果您将UITextView与其内容合并按键动作识别与动态大小你应该有它!


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. (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. I'd like to implement the same behavior for my Cocoa app.

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

So if you combine the keystroke recognition with the dynamic sizing you should have it!!!

这篇关于我如何使Cocoa NSTextView成长为用户键入它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆