目标C语言 NSView调整大小



我有一个NSView里面有一个NSTextField。问题是,每次在显示视图之前,NSTextField的大小都会改变我想要NSView的大小随之改变。因此,如果textfield是2行长,nsview将是小的(只是勉强包围textfield),但当它的10行长,我不希望视图的大小切断textfield,我希望视图增长与它。

我该怎么做呢?谢谢。

这是我在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.

希望有帮助!

您需要设置" contenthug和contentCompression"的优先级。

简而言之:

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等…剪辑(例如,当文本字段有"一些testValue"变成"一些testV…"等)

最新更新