NSPopUpButton箭头颜色

时间:2012-06-01 19:00:30

标签: cocoa colors nspopupbutton

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

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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];
}

答案 2 :(得分: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.
}

答案 3 :(得分: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]
}
}

答案 4 :(得分:0)

Swift 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)
    }
}