0
votes

I have an NSView with a NSTextField in it. The problem is, each time before displaying the view, the size of the NSTextField can change and I want the size of NSView to change with it. So if textfield is 2 lines long, the nsview will be small (just barely surround the textfield) but when its 10 lines long, I don't want the view's size to cut off the textfield, i want the view to grow with it.

How can I do that? Thanks.

2
I think you are going to need to add some more info.In example how is the text being entered, does it contain new lines,do you want the width, height or both to change. My thought is you will need to count the length of characters and adjust the rect of the view by that. I.e each line is 20 characters long, every 20 characters increase sizemarkhunte

2 Answers

0
votes

This is what I use in my tableview, but it should apply equally to your situation:

    float textLabelWidth = 190;

    NSString *Text = [[deals objectAtIndex:[indexPath section]] objectForKey:@"title"];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:13.0];
    CGSize constraintSize = CGSizeMake(textLabelWidth, MAXFLOAT);
    CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20; // <--- this is what your NSView's height should be, just edit the textLabelWidth and padding (in this case 20) to whatever you desire.

Hope that helps!

0
votes

Your need to set the "contentHugging and contentCompression" priorities.

In short:

contentHugging: sets the preference where a control resists being made *larger* than it's intrinsic size

contentCompression: sets the preference where a control resists being made *smaller* than it's intrinsic size

Apple have a reference here:

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithConstraintsinInterfaceBuidler.html

Specifically scroll down to the Setting Content-Hugging and Compression-Resistance Priorities section.

This is what deals with textFields etc... clipping (i.e. when a textfield has "some testValue" becomes "some testV..." etc...