• 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
Resizing custom subclassed NSTextView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Resizing custom subclassed NSTextView


  • Subject: Resizing custom subclassed NSTextView
  • From: Joe White <email@hidden>
  • Date: Mon, 06 Jun 2011 23:29:38 +0100

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

  • Follow-Ups:
    • Re: Resizing custom subclassed NSTextView
      • From: "Gary L. Wade" <email@hidden>
    • Re: Resizing custom subclassed NSTextView
      • From: Ross Carter <email@hidden>
  • Prev by Date: Re: tools for writing help books
  • Next by Date: Re: tools for writing help books
  • Previous by thread: Re: List of fonts installed?
  • Next by thread: Re: Resizing custom subclassed NSTextView
  • Index(es):
    • Date
    • Thread