NSView 调整大小

发布于 2024-10-31 07:41:20 字数 197 浏览 4 评论 0原文

我有一个 NSView ,其中有一个 NSTextField 。问题是,每次显示视图之前, NSTextField 的大小都会改变,我希望 NSView 的大小也随之改变。因此,如果文本字段有 2 行长,则 nsview 会很小(只是勉强包围文本字段),但是当其长度为 10 行时,我不希望视图的大小切断文本字段,我希望视图随之增长。

我怎样才能做到这一点?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-11-07 07:41:20

这是我在表格视图中使用的,但它应该同样适用于您的情况:

    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.

希望有帮助!

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!

眼藏柔 2024-11-07 07:41:20

您需要设置“contentHugging 和 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

苹果在这里有一个参考:

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

具体向下滚动到设置内容拥抱和压缩阻力优先级部分。

这就是处理文本字段等...剪切(即当文本字段具有“某些 testValue”变为“某些 testV ...”等时...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文