5

有没有办法自定义 NSPopUpButton 箭头的颜色?我环顾四周,但我还没有找到答案

4

5 回答 5

1

我真的不认为有一种“简单”的方法可以做到这一点。如果您查看 API 描述,它甚至声明它不响应 setImage 例程。我已经做了很多工作,对按钮对象等进行子分类......我认为这是你必须去的地方才能做你所要求的。

于 2012-06-01T19:14:11.533 回答
0

就像这些控件中的太多一样,我通过继承 NSPopupButton(Cell) 然后在 drawRect 中完成我自己的所有绘图来做到这一点......虽然我作弊了一点,并使用图像来做实际的三角形而不是试图通过原语来做.

- (void)drawRect:(NSRect)dirtyRect 
{
  //...Insert button draw code here...
  //Admittedly the above statement includes more work than we probably want to do.
  //Assumes triangleIcon is a cached NSImage...I also make assumptions about location

  CGFloat iconSize = 6.0;
  CGFloat iconYLoc = (dirtyRect.size.height - iconSize) / 2.0;
  CGFloat iconXLoc = (dirtyRect.size.width - (iconSize + 8));

  CGRect triRect = {iconXLoc, iconYLoc, iconSize, iconSize};
  [triangleIcon drawInRect:triRect];
}
于 2015-02-26T23:21:39.697 回答
0

我通过使用“假色”过滤器更改了箭头颜色,而不使用任何图像。到目前为止,这是改变可可控制对我来说最简单的方法。

class RLPopUpButton: NSPopUpButton {
init() {
    super.init(frame: NSZeroRect, pullsDown: false)
    addFilter()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    addFilter()
}

func addFilter() {
    let colorFilter = CIFilter(name: "CIFalseColor")!
    colorFilter.setDefaults()
    colorFilter.setValue(CIColor(cgColor: NSColor.black.cgColor), forKey: "inputColor0")
    colorFilter.setValue(CIColor(cgColor: NSColor.white.cgColor), forKey: "inputColor1")

//        colorFilter.setValue(CIColor(cgColor: NSColor.yellow.cgColor), forKey: "inputColor0")
//        colorFilter.setValue(CIColor(cgColor: NSColor.property.cgColor), forKey: "inputColor1")

    self.contentFilters = [colorFilter]
}
}
于 2018-09-18T14:09:15.290 回答
0

我这样做了,它对我有用。

(void)drawImageWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSPopUpButton *temp = (NSPopUpButton*)controlView;

    NSString *strtile = temp.title;

    AppDelegate *appdel = (AppDelegate*)[NSApplication sharedApplication].delegate;
    NSFont *font = [NSFont systemFontOfSize:13.5];
   NSSize size = NSMakeSize(40, 10);// string size

    CGRect rect = controlView.frame;
    rect = CGRectMake((size.width + temp.frame.size.width)/2, rect.origin.y, 8, 17);

    [self drawImage:[NSImage imageNamed:@"icon_downArrow_white.png"] withFrame:rect inView:self.
}
于 2017-11-27T11:20:05.320 回答
0

斯威夫特 5

在界面生成器中,删除默认箭头设置。然后,为单元格应用这个子类,这将在 NSPopUpButton 的右侧添加一个 NSImageView。

这样,您就可以完全控制设置为自定义按钮的内容以及定位方式。

import Cocoa

@IBDesignable class NSPopUpButtonCellBase: NSPopUpButtonCell {
    
    let textColor = NSColor(named: "white")!
    let leftPadding: CGFloat = 16
    let rightPadding: CGFloat = 30

    override func awakeFromNib() {
        super.awakeFromNib()
        
        let imageView = NSImageView()
        imageView.image = NSImage(named: "ic_chevron_down")!

        controlView!.addSubview(imageView)
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        
        imageView.widthAnchor.constraint(equalToConstant: CGFloat(20)).isActive = true
        imageView.heightAnchor.constraint(equalToConstant: CGFloat(20)).isActive = true
        imageView.trailingAnchor.constraint(equalTo: controlView!.trailingAnchor).isActive = true
        imageView.centerYAnchor.constraint(equalTo: controlView!.centerYAnchor).isActive = true
    }
    
    // overriding this removes the white container
    override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
        
    }
    
    // overriding this allows us to modify paddings to text
    override func titleRect(forBounds cellFrame: NSRect) -> NSRect {
        // this gets rect, which has title's height, not the whole control's height
        // also, it's origin.y is such that it centers title
        let processedTitleFrame = super.titleRect(forBounds: cellFrame)

        let paddedFrame = NSRect(
            x: cellFrame.origin.x + leftPadding,
            y: processedTitleFrame.origin.y,
            width: cellFrame.size.width - leftPadding - rightPadding,
            height: processedTitleFrame.size.height
        )

        return paddedFrame
    }

    // overriding this allows us to style text
    override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
        let attributedTitle = NSMutableAttributedString.init(attributedString: title)
        let range = NSMakeRange(0, attributedTitle.length)
        attributedTitle.addAttributes([NSAttributedString.Key.foregroundColor : textColor], range: range)

        return super.drawTitle(attributedTitle, withFrame: frame, in: controlView)
    }
}
于 2021-02-12T11:38:37.030 回答