NSView调整大小

时间:2011-04-09 07:26:32

标签: objective-c cocoa nsview nstextfield

我有一个带有NSTextField的NSView。问题是,每次显示视图之前,NSTextField的大小都可以改变,我希望NSView的大小随之改变。因此,如果textfield是2行长,nsview将很小(只是几乎不包围文本字段)但是当它的10行长时,我不希望视图的大小切断文本字段,我希望视图随之增长。

我该怎么做?感谢。

2 个答案:

答案 0 :(得分:0)

这是我在tableview中使用的内容,但它应该同样适用于您的情况:

    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.

希望有所帮助!

答案 1 :(得分:0)

您需要设置&#34; contentHugging和contentCompression&#34;优先级。

简而言之:

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在这里有一个参考:

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

专门向下滚动到Setting Content-Hugging and Compression-Resistance Priorities部分。

这就是处理textFields等...剪辑(即当文本字段有&#34;某些testValue&#34;变成&#34;某些testV ...&#34;等...