使用全宽的NSPopupButton作为文本,没有箭头 [英] Using full width of NSPopupButton for text, no arrows

查看:285
本文介绍了使用全宽的NSPopupButton作为文本,没有箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢显示没有箭头的 NSPopupButton 。我喜欢使用那个空间,箭头将是标签文本。



这里是启用箭头的文本(即01234567890):





这里没有箭头:





正如你所看到的,箭头占据的空间不会被文本使用。 p>

如何使用文本的全部内容宽度?



我使用F-Script检查控件但发现控件的subviews数组看起来是空的 - 我希望找到两个子视图,如果是这样,我会删除一个箭头,并使文本更宽。

解决方案

一种方法是创建一个 NSPopupButtonCell 子类, drawTitle:withFrame:inView:方法。



这是它的精华:

  @interface MyCell:NSPopUpButtonCell 
@property NSDictionary< NSString *,id> * attrs; (NSRect)的标题框架(NSRect)框架inView:(NSView *)controlView $($)$($)





$ b b $ b {
CGSize cs = controlView.bounds.size;
CGPoint or = frame.origin;
CGSize fs = frame.size;
const int th = fs.height;
const int inset = th * 2/3;
const int indent = self.image? th + 6:0; //假设img在文本的左边,我们将缩进文本
frame = CGRectMake(inset + indent,or.y,cs.width-2 * inset-indent,fs.height);
return [super drawTitle:title withFrame:frame inView:controlView];
}

@end

注意:此代码不覆盖了向菜单项添加图标的所有可能的情况 - 它只处理左侧有一个适配图标的最常见的情况,并且由于使用通过试错法发现的缩进值仍然有点。。。



另请参阅@NSGod关于在您的笔尖或故事板中设置单元格类别的注释。



更新:优化代码只需要 NSPopUpButtonCell 的子类,而不是 NSPopUpButton <

更新2:代替实现 drawTitleWithFrame:inView:/ code>,


$ b <
我现在覆盖 drawTitle:withFrame:inView:,因为这给我正确地居中的框架绘制和使整个代码更短。

I like to display an NSPopupButton that has no arrows. I like to use that space where the arrows would be for the label text. That doesn't seem to work by default.

Here's the text (which is "01234567890") with the arrows enabled:

And here with no arrows:

As you can see, the space the arrows would occupy is not used by the text.

How can I use the full content width for the text?

I've inspected the control with F-Script but found that the control's subviews array appears to be empty - I had expected to find two subviews, and if that were the case, I'd remove the one for the arrows and make the one for the text wider.

解决方案

One way to accomplish this is to create an NSPopupButtonCell subclass and draw the text yourself in its drawTitle:withFrame:inView: method.

Here's the essence of it:

@interface MyCell : NSPopUpButtonCell
    @property NSDictionary<NSString*,id> *attrs;
@end

@implementation MyCell

-(NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
{
    CGSize cs = controlView.bounds.size;
    CGPoint or = frame.origin;
    CGSize fs = frame.size;
    const int th = fs.height;
    const int inset = th * 2 / 3;
    const int indent = self.image ? th+6 : 0; // assuming the img is on the left of the text, we'll indent the text
    frame = CGRectMake (inset+indent, or.y, cs.width-2*inset-indent, fs.height);
    return [super drawTitle:title withFrame:frame inView:controlView];
}

@end

Note: This code does not cover all possible cases of adding an icon to the menu items - it only handles the most common case of having a fitting icon on the left, and that's still a bit wonky because of using indenting values I found by trial-and-error. Adjust them yourself as needed.

See also the comment below by @NSGod about setting up the cell's class in your nib or storyboard.

Update: Optimized the code to need only a subclass of the NSPopUpButtonCell but not of NSPopUpButton, thanks to @NSGod.

Update 2: Instead of implementing drawTitleWithFrame:inView: I now overwrite drawTitle:withFrame:inView: because that gives me the correctly centered frame for drawing and makes the whole code shorter.

这篇关于使用全宽的NSPopupButton作为文本,没有箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆