Re: Resizing custom subclassed NSTextView
Re: Resizing custom subclassed NSTextView
- Subject: Re: Resizing custom subclassed NSTextView
- From: Ross Carter <email@hidden>
- Date: Wed, 08 Jun 2011 20:51:21 -0400
On Jun 8, 2011, at 7:19 PM, Joe White wrote:
> Basically, the interface is separate editable objects that are usually single symbols with a few arguments included. This means that the each object is always one line high and usually not that wide.
>
> I started off using NSTextField (which I could resize easily) but I want to add syntax highlighting and auto-completion. From what I read NSTextView is the way forward.
First let me say that I have not done what you are doing, so my advice is a mere guess. Others may have better advice.
It seems to me that you need to use NSTextField objects and subclass the field editor to provide the custom behavior you need. It might be easiest to get one text view working the way you want and then look into using that text view class as the field editor.
> Thanks for the code, it does indeed resize for me. However it doesn't shrink when I remove text.
>
> From poring over the Apple docs again (http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html) I managed to get my the NSTextView subclass to resize. However it doesn't redraw itself and looks weird until I change the size of the main canvas.
Right, I forgot about that. The textView is resizing when you remove characters, but its superview is not updating. If you observe the NSViewFrameDidChangeNotification for the textView, you can update the display of its superview and watch the textView grow and shrink.
[tv setPostsFrameChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewFrameDidChange:) name:NSViewFrameDidChangeNotification object:tv];
- (void)textViewFrameDidChange:(NSNotification *)note {
[[[note object] superview] setNeedsDisplayInRect:<calculate a reasonable rect here>];
}
> Would you say I am taking the wrong approach? All I wish to do is to have multiple instances of an object each with its own resizable/auto completing text field as a subview.
If you've got syntax highlighting and auto-correction working the way you want, I'd say you're most of the way there. If you have only a few text objects or if the app is not for production, text views are fine. In production, I'd want to use text fields instead of text views to avoid the overhead of having a layout manager for every one of those text objects.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden