Re: Resizing custom subclassed NSTextView
Re: Resizing custom subclassed NSTextView
- Subject: Re: Resizing custom subclassed NSTextView
- From: "Gary L. Wade" <email@hidden>
- Date: Wed, 08 Jun 2011 18:30:05 -0700
If you're fine with a single line that grows horizontally, try using an NSTextField object in single-line mode, and utilize textDidChange:, calling sizeToFit on the object. The method sizeToFit is a method of NSControl, not NSTextView or its superclasses. I'm suggesting all this from memory, though, but that should get you going in the right direction. You shouldn't need to subclass NSTextField to do this.
- Gary L. Wade (Sent from my iPad)
On Jun 6, 2011, at 3:29 PM, Joe White <email@hidden> wrote:
> Hi,
>
> I currently have a custom subclassed NSTextView as a subview of an NSView
> (which is its delegate).
>
> I'm trying to to dynamically resize the minimum width of the NSTextView
> based on the string input so that view resizes to fit (the height is always
> one line).
>
> This is the NSTextView init:
>
> - (id)initWithFrame:(NSRect)frame {
>
> self = [super initWithFrame:frame];
>
> if (self != nil) {
>
> [self setFont:[NSFont fontWithName:@"Helvetica" size:self.frame.size.
> height - 10]];
>
> [self setSelectable:YES];
>
> [self setEditable:YES];
>
> [[self textContainer] setContainerSize:NSMakeSize(FLT_MAX, self.frame.
> size.height)];
>
> [self setHorizontallyResizable:YES];
>
> [self setVerticallyResizable:NO];
>
> }
>
> return self;
>
> }
>
>
> I tried the following on didChangeText to no avail (the text container's
> maximum width is set to FLT_MAX and isHorizontallyResizable is true):
>
> - (void)didChangeText {
>
> [self sizeToFit];
>
> [self setNeedsDisplay:YES];
>
> }
>
> After reading these articles:
> -
> http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextStorageLayer/Tasks/TrackingSize.html#//apple_ref/doc/uid/20000927-CJBBIAAF
> -
> http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html#//apple_ref/doc/uid/20001809-CJBGBIBB
>
> It seems like I have to use the NSTextView's layout manager to force
> resizing of the text view. So I tried adding the following to didChangeText
> (although I'm sure if that is the right place).
>
> (void) [[self layoutManager] glyphRangeForTextContainer:[self
> textContainer]];
>
> NSLog(@"%f",[[self layoutManager] usedRectForTextContainer:[self
> textContainer]].size.width);
>
>
> When I input text the log just prints out the initial width of the text
> view.
>
> I'm not sure where I'm going wrong with this, any help or pointers would be
> much appreciated.
>
> Thanks,
> Joe
> --
> Joe White
> Production Department, RjDj
>
> Tel : +44 7515 731499
>
> Reality Jockey Ltd.
> Floor B “The Mission”
> 55 Holywell Lane
> London
> EC2A 3PQ
_______________________________________________
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