Getting an NSTextView to Fully Match Its Content

I have a view containing a button and a text view. When the button is pressed, the hidden status of the text will be changed and displayed on the screen. Springs and spacers are configured so that the text expands vertically with a view. All this is done in IB

Then I insert the text into the text image programmatically, but I need the text image to display all its contents without the need for scrolling.

This is the code that I use to calculate the height of the text in text form:

- (float) getTextViewHeight { //based on http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html%23//apple_ref/doc/uid/20001809-CJBGBIBB [textview.textContainer setLineFragmentPadding:0.0]; [textview.layoutManager glyphRangeForTextContainer:textview.textContainer]; return [textview.layoutManager usedRectForTextContainer:self.interactionData.textContainer].size.height; } 

With a call to -sizeToFit in text form or without it, it will be either too large or too small (depending on its contents).

I need to get the height of the text view with all the content so that I can adjust the size of the view.

I know that I could use NSTextField as a label, but I need an NSTextView for its added functionality (in particular, using the control built into scrollview).

Does anyone have any suggestions?

+4
source share
3 answers

NSTextView will usually resize if its string exceeds the width of the container. I think this is because the contained cell has a default behavior for overspending text called "Line Wrap" or something like that. I feel that you can simply ask the TextView about its height after loading it and adjust the containing view accordingly, without the need for a layout manager. And, obviously, make sure the mask is set to automatically resize (oh, you do this in IB, so don't worry). I could be wrong, and I did not do any tests ... but yes, you could try !: P

+2
source

Her how I do it works well:

 // Help text. NSBundle* mainBundle = [NSBundle mainBundle]; NSString* path = [mainBundle pathForResource: @"category-analysis-help" ofType: @"rtf"]; NSAttributedString* text = [[NSAttributedString alloc] initWithPath: path documentAttributes: NULL]; [helpText setAttributedStringValue: text]; NSRect bounds = [text boundingRectWithSize: NSMakeSize(helpText.bounds.size.width, 0) options: NSStringDrawingUsesLineFragmentOrigin]; helpContentView.frame = NSMakeRect(0, 0, helpText.bounds.size.width + 20, bounds.size.height + 20); 

helpContentView is just a container for helpText that adds some margin around the text. helpText resizes using the container.

It should be obvious that a fixed width is needed for the correct height, since the height depends on what is suitable for the lines.

+1
source

If you want to completely omit the scroll view (for example, make a text view attached to another supervisor and match its text itself), you can take a look at NSText. This, AFAICT, is basically an NSTextView without a supervisor (part of the scroll) and can automatically resize.

-1
source

All Articles