如何编码 NSButton 看起来就像图像 [英] how to code NSButton to look just like image

查看:86
本文介绍了如何编码 NSButton 看起来就像图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用代码(不是界面构建器)我需要创建一个看起来像图像的 NSButton.具体来说,我想使用 NSImageNameStopProgressFreestandingTemplate 并且我需要它不是看起来像按钮而是看起来像图像.这意味着:
1.没有按钮按下"的外观
2. 无边框,按钮不可见

Using code (not the Interface builder) I need to create an NSButton that looks like an image. Specifically I want to use NSImageNameStopProgressFreestandingTemplate and I need it not to look like button but to look like the image. This means:
1. No 'button down' look
2. No border, no any visibility of the button

谢谢.

推荐答案

我知道这个回复有点晚了,但你可以试试这个,给定 thisButton:

I know this response is a bit late, but you could try this, given thisButton:

[thisButton setImage:[NSImage imageNamed:NSImageNameStopProgressFreestandingTemplate]];
[thisButton setImagePosition:NSImageOnly];
[thisButton setBordered:NO];

最后一行是关键点:移除按钮边框有效地剥离了它的边框,只留下可以点击的图像.(顺便说一句,我还没有专门尝试过上面的代码,因此您可能需要进行一些其他调整,例如设置 imageScalingbuttonType,以获得效果最好.)

That last line is the key bit: removing the button border effectively strips it of its bezel, leaving only the image to click on. (BTW, I haven't tried the above code specifically, so you may need to throw in a couple of other tweaks, such as setting the imageScaling or buttonType, to get it to work best.)

最后一点:如果您使用的是模板图像(正如您所说的那样),Cocoa 将自动以轻微的深灰色渐变显示它;单击按钮时,它会暂时变暗为纯黑色.这是您不想要的自动按钮按下"外观;然而,它非常微妙,是按钮工作的一个很好的指标.如果您不希望发生这种情况,您可以获取所需图像的实例并在其上[stopImage setTemplate:NO];.

One final note: If you're using a template image (as you said you would), Cocoa will automatically display it with a slight dark-grey gradient; when the button is clicked, it will momentarily darken to solid black. This is an automatic "'button down' look" you didn't want; however, it is very subtle, and is a good indicator that the button worked. If you don't want this to happen, you could get an instance of the desired image and [stopImage setTemplate:NO]; on it.

这篇关于如何编码 NSButton 看起来就像图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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