• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Resizing custom subclassed NSTextView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Resizing custom subclassed NSTextView


  • Subject: Re: Resizing custom subclassed NSTextView
  • From: Ross Carter <email@hidden>
  • Date: Wed, 08 Jun 2011 14:05:56 -0400

On Jun 6, 2011, at 6:29 PM, Joe White 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.

You don't need to override -didChangeText. In fact you don't need to subclass NSTextView.

If you create a new project in Xcode, and add the following code to your application delegate class, you will see a textView that expands horizontally as you type. I think that is what you want.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	NSTextView *tv = [[NSTextView alloc] initWithFrame:NSMakeRect(100, 100, 100, 25)];
	[tv setHorizontallyResizable:YES];
	[tv setVerticallyResizable:NO];
	[tv setMaxSize:NSMakeSize(500, 25)];

	[[self.window contentView] addSubview:tv];
	[tv release];

	NSTextContainer *tc = [tv textContainer];
	NSSize tcSize = [tc containerSize];
	tcSize.width = 1.0e6;
	[tc setContainerSize:tcSize];
	[tc setWidthTracksTextView:NO];
}

I assume you have a plan in place to deal with situations where the textView at maximum width cannot display all the characters entered (or pasted) by the user.

_______________________________________________

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

  • Follow-Ups:
    • Re: Resizing custom subclassed NSTextView
      • From: Joe White <email@hidden>
References: 
 >Resizing custom subclassed NSTextView (From: Joe White <email@hidden>)

  • Prev by Date: Cocoaheads Lake Forest meeting TONIGHT, 7pm, El Toro library
  • Next by Date: Re: Login item not hidden
  • Previous by thread: Resizing custom subclassed NSTextView
  • Next by thread: Re: Resizing custom subclassed NSTextView
  • Index(es):
    • Date
    • Thread