How to resize NSTextView according to its content?

Discussion in 'Mac Programming' started by Monaj, Apr 16, 2010.

  1. Monaj macrumors regular

    May 24, 2009
    Hi all,

    I am trying to set an attributed string within NSTextView. I want to increase its height based on its content, initially it is set to some default value.

    So I tried this method:

    I set content in NSTextView. When we set some content in NSTextView its size automatically increases. So I increased height of its super view that is NSScrollView to its height, but NSScrollView is not getting completely resized, it is showing scroller on right.

    The code I used is:

    float xCoordinate = 15.0;
    [xContentViewScroller setFrame:NSMakeRect(xCoordinate, 0.0, 560.0, 10.0)];
    [[xContentView textStorage] setAttributedString:xContents];
    float xContentViewScrollerHeight = [xfContentView frame].size.height + 2;
    [xContentViewScroller setFrame:NSMakeRect(xCoordinate, 0.0, 560.0, xContentViewScrollerHeight)]; 
    Can anyone suggest me some way or method to resolve this issue. By doing google search I found that in UITextView there is contentSize method which can get size of its content, I tried to find similar method in NSTextView but could not get any success :(


  2. Sydde macrumors 68020


    Aug 17, 2009
    If you want to omit the scroll view entirely (e.g., make a text view that is attached to another superview and sizes itself to fit its text), you might take a look at NSText. It is, AFAICT, basically a NSTextView without the superview (scroll view parts), and can automagically resize itself.
  3. Monaj thread starter macrumors regular

    May 24, 2009
    Thanks Sydde for your reply..

    Can you please throw some light on how to use NSText object? From its class reference I got that : You usually use instances of its subclass, NSTextView. So I am becoming clueless on how will I represent it in IB and will use it according to my requirements? :confused:

    Once again thanks for helping a newbie.
  4. Sydde macrumors 68020


    Aug 17, 2009
    To create an NSText in a nib, drop a custom view onto your window, then go to the identity tab in the inspector panel and set its class. In fact, if you set the class to NSTextView, you will get the same result: a text view without the scrollbars that autoresizes itself. However, neither of these work quite as you would hope in Cocoa Simulator, you will probably have to do something in your -awakeFromNib or in your app code to get them to behave properly.

Share This Page