gpt4 book ai didi

macos - NSPopUpButton 白色文本

转载 作者:搜寻专家 更新时间:2023-11-01 05:31:24 24 4
gpt4 key购买 nike

我在我的应用程序中的自定义 View 中创建了一个 NSPopUpButton,该 View 具有黑色填充。我现在想让 NSPopUpButton 具有白色文本颜色,但我似乎无法做到这一点。我应该怎么做?

谢谢

最佳答案

这很简单。你只需要重写-[NSPopUpButtonCell drawTitle:withFrame:inView],然后就可以改变关于标题的一切,比如颜色、字体、框架等等。下面是一个例子。希望对您有所帮助。

目标-C:

@interface MyPopUpButtonCell : NSPopUpButtonCell

@property (nonatomic, readwrite, copy) NSColor *textColor;

@end

@implementation MyPopUpButtonCell

- (NSRect)drawTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)controlView {
NSRect newFrame = NSMakeRect(NSMinX(frame), NSMinY(frame)+2, NSWidth(frame), NSHeight(frame));
if (self.textColor) {
NSMutableAttributedString *newTitle = [[NSMutableAttributedString alloc] initWithAttributedString:title];
NSRange range = NSMakeRange(0, newTitle.length);
[newTitle addAttribute:NSForegroundColorAttributeName value:self.textColor range:range];
return [super drawTitle:newTitle withFrame:newFrame inView:controlView];
}else {
return [super drawTitle:title withFrame:newFrame inView:controlView];
}
}

@end

swift :

class MyPopUpButtonCell: NSPopUpButtonCell {
var textColor: NSColor? = nil

override
func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
if self.textColor != nil {
let attrTitle = NSMutableAttributedString.init(attributedString: title)
let range = NSMakeRange(0, attrTitle.length)
attrTitle.addAttributes([NSAttributedString.Key.foregroundColor : self.textColor!], range: range)
return super.drawTitle(attrTitle, withFrame: frame, in: controlView)
}else {
return super.drawTitle(title, withFrame: frame, in: controlView)
}
}
}

关于macos - NSPopUpButton 白色文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30548730/

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