gpt4 book ai didi

objective-c - 突出显示 NSImageView - Objective-c

转载 作者:行者123 更新时间:2023-12-03 16:50:55 32 4
gpt4 key购买 nike

有没有办法用Mac的默认突出显示颜色“突出显示”NSImageView?我只是在寻找一种方法来为我的 nsimageview 着色,以便用户可以轻松识别该对象。

谢谢

凯文

最佳答案

如果 NSImageView 的内置选项之一不够,您可以继承 NSImageView 的子类,并在 drawRect 中执行以下操作:

- (void)drawRect:(NSRect)frame {
[super drawRect:frame]; // this takes care of image
[NSBezierPath setDefaultLineWidth:4.0];
[[NSColor highlightColor] set];
[NSBezierPath strokeRect:frame]; // will give a 2 pixel wide border
}

啊,为了将其作为变量状态,我可能会定义一个实例变量,例如 isHighlighted 来跟踪该状态。然后,每当发生任何会改变突出显示状态的事情时,您都可以将 View 设置为需要重新显示。例如,您可以在 set/get 方法中执行此操作:

- (void)setHighlighted:(BOOL)aHighlighted {
isHighlighted = aHighlighted;
[self setNeedsDisplay:YES];
}

然后,更新您的drawRect:方法以考虑isHighlighted标志。如何实现不突出显示的外观可能取决于 ImageView 的风格。您可以尝试仅调用 super 来进行绘图,但如果在测试中,您看到 super 的绘图未覆盖的任何残留或杂散突出显示的像素信息,您可能需要首先绘制清晰的颜色,然后调用 super。

所以,像这样:

- (void)drawRect:(NSRect)frame {
isHighlighted ? [[NSColor highlightColor] set] : [[NSColor clearColor] set];
[NSBezierPath setDefaultLineWidth:4.0];
if (isHighlighted) {
[super drawRect:frame];
[NSBezierPath strokeRect:frame]; // will give a 2 pixel wide border
} else {
[NSBezierPath fillRect:frame];
[super drawRect:frame];
}
}

关于objective-c - 突出显示 NSImageView - Objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4309005/

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