gpt4 book ai didi

cocoa - 将 NSPopupButton 的大小调整为其选定的标题

转载 作者:行者123 更新时间:2023-12-03 16:27:11 26 4
gpt4 key购买 nike

我有一个 NSPopupButton,我想要它调整自身大小以适合所选标题。

[NSPopupButton sizeToFit] 不符合我的需求,因为弹出窗口的大小调整为最大的标题项,而不是当前选定的标题项

我尝试了很多方法,但都没有成功

#define ARROW_WIDTH 20
NSDictionary *displayAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[popup font], NSFontAttributeName, nil];
NSSize titleSize = [popup.titleOfSelectedItem sizeWithAttributes:displayAttributes] + ARROW_WIDTH;

但是常量值 ARROW_WIDTH 是一个非常肮脏且容易出错的解决方案。

状态栏上的 TextWrangler 编码组合就像我需要的那样

最佳答案

我处理文本字段这些问题的方法是尝试使用从未添加到 View 层次结构中的文本字段来调整大小。您对不打算重用的对象调用 sizeToFit,然后使用它来确定实际控件需要多宽才能适合您需要执行的操作。

因此,在伪代码中,您可以这样做(假设您将 ARC、YMMV 用于非 ARC 项目,因为这会泄漏):

NSArray *popupTitle = [NSArray arrayWithObject: title];
NSPopUpButton *invisiblePopup = [[NSPopUpButton alloc] initWithFrame: CGRectZero pullsDown: YES];
// Note that you may have to set the pullsDown bool to whatever it is with your actual popup button.
[invisiblePopup addItemWithTitle: @"selected title here"];
[invisiblePopup sizeToFit];
CGRect requiredFrame = [invisiblePopup frame];
self.actualPopup.frame = requiredFrame;

关于cocoa - 将 NSPopupButton 的大小调整为其选定的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13539378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com