NSButton渐变样式

发布于 2024-12-31 23:12:19 字数 1934 浏览 3 评论 0原文

在 Interface Builder 组件中,我有渐变按钮

Gradient Button

我想以编程方式创建此按钮。但我找不到按钮类型、边框样式和渐变类型的组合来重现它。我的本地目标是创建像这样的加号(添加)和减号(删除)按钮

img src="https://i.sstatic.net/ks4Ug.png" alt="添加和删除项目,渐变按钮">

< 您还可以知道如何实现减号右边的部分吗?它看起来像禁用的渐变按钮。

更新(2012-02-01, 14:52)

我将渐变按钮放在空的xib上,用文本编辑器打开xib并找到我的按钮。这是代码的一部分。

<array class="NSMutableArray" key="NSSubviews">
  <object class="NSButton" id="155381693">
    <reference key="NSNextResponder" ref="1005"/>
    <int key="NSvFlags">268</int>
    <string key="NSFrame">{{20, 303}, {106, 23}}</string>
    <reference key="NSSuperview" ref="1005"/>
    <reference key="NSWindow"/>
    <string key="NSReuseIdentifierKey">_NS:2466</string>
    <bool key="NSEnabled">YES</bool>
    <object class="NSButtonCell" key="NSCell" id="976550657">
      <int key="NSCellFlags">-2080244224</int>
      <int key="NSCellFlags2">134217728</int>
      <string key="NSContents">Gradient Button</string>
      <object class="NSFont" key="NSSupport">
        <string key="NSName">LucidaGrande</string>
        <double key="NSSize">13</double>
        <int key="NSfFlags">1044</int>
      </object>
      <string key="NSCellIdentifier">_NS:2466</string>
      <reference key="NSControlView" ref="155381693"/>
      <int key="NSButtonFlags">-2033434369</int>
      <int key="NSButtonFlags2">162</int>
      <string key="NSAlternateContents"/>
      <string key="NSKeyEquivalent"/>
      <int key="NSPeriodicDelay">400</int>
      <int key="NSPeriodicInterval">75</int>
    </object>
  </object>
</array>

我将尝试使用此信息重现按钮。

In Interface Builder components I have Gradient Button

Gradient Button

I would like to create this button programmatically. But I can't find combination of button type, bezel style and gradient type to reproduce it. My local goal is to create plus (add) and minus (remove) buttons like this

Add and remove items, gradient button

May be you can also know how to implement part at right of minus? It looks like disabled gradient button.

Update (2012-02-01, 14:52)

I put gradient button on empty xib, open xib with text editor and find my button. This is a part of code.

<array class="NSMutableArray" key="NSSubviews">
  <object class="NSButton" id="155381693">
    <reference key="NSNextResponder" ref="1005"/>
    <int key="NSvFlags">268</int>
    <string key="NSFrame">{{20, 303}, {106, 23}}</string>
    <reference key="NSSuperview" ref="1005"/>
    <reference key="NSWindow"/>
    <string key="NSReuseIdentifierKey">_NS:2466</string>
    <bool key="NSEnabled">YES</bool>
    <object class="NSButtonCell" key="NSCell" id="976550657">
      <int key="NSCellFlags">-2080244224</int>
      <int key="NSCellFlags2">134217728</int>
      <string key="NSContents">Gradient Button</string>
      <object class="NSFont" key="NSSupport">
        <string key="NSName">LucidaGrande</string>
        <double key="NSSize">13</double>
        <int key="NSfFlags">1044</int>
      </object>
      <string key="NSCellIdentifier">_NS:2466</string>
      <reference key="NSControlView" ref="155381693"/>
      <int key="NSButtonFlags">-2033434369</int>
      <int key="NSButtonFlags2">162</int>
      <string key="NSAlternateContents"/>
      <string key="NSKeyEquivalent"/>
      <int key="NSPeriodicDelay">400</int>
      <int key="NSPeriodicInterval">75</int>
    </object>
  </object>
</array>

I will try to reproduce button using this information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2025-01-07 23:12:19

天哪,我瞎了。只需将边框样式设置为 NSSmallSquareBezelStyle 和按钮
键入 NSMomentaryPushInButton。

我不需要图像。我不需要使用 CoreGraphics 进行自定义绘制(我为此做了我自己的类)。我不需要解析 xib。我不需要转储运行时结构(我这样做是为了找到使用 xib 创建的渐变按钮的出口中字段的必要值)。我必须尝试 文档。但是,如果您阅读文档中 NSSmallSquareBezelStyle 的描述,您就会理解我的困惑:它与 IB 向导中的“渐变”值相差甚远。

在此处输入图像描述

正确代码:

NSButton *gradientButton = [[NSButton alloc] initWithFrame:NSMakeRect(40, 40, 80, 32)];
[gradientButton setButtonType:NSMomentaryPushInButton];
[gradientButton setBezelStyle:NSSmallSquareBezelStyle];
[gradientButton setTitle:@"Title"];
[self.window.contentView addSubview:gradientButton];

结果:

在此处输入图像描述

OMG, I was blind. Just set bezel style to NSSmallSquareBezelStyle and button
type to NSMomentaryPushInButton.

I don't need images. I don't need custom draw with CoreGraphics (I did my own class for that). I don't need to parse xib. I don't need to dump runtime structures (I did it to find nessesuary values of fields in outlet to gradient button created with xib). I had to try all values of bezel style from documentation. But if you read description for NSSmallSquareBezelStyle in documentation you understand my confusion: it's very far from "Gradient" value in IB wizard.

enter image description here

Correct code:

NSButton *gradientButton = [[NSButton alloc] initWithFrame:NSMakeRect(40, 40, 80, 32)];
[gradientButton setButtonType:NSMomentaryPushInButton];
[gradientButton setBezelStyle:NSSmallSquareBezelStyle];
[gradientButton setTitle:@"Title"];
[self.window.contentView addSubview:gradientButton];

Result:

enter image description here

新雨望断虹 2025-01-07 23:12:19

我相信您引用的按钮是一个自定义按钮,并且可能是一个自定义背景图像,尽管它肯定可以通过编程方式创建。这些类型的按钮的问题在于,该按钮有多种变体,它们看起来非常相似,但有细微的差别。

创建这些按钮的最简单方法是使用渐变按钮并将 Xcode 库存图标中的 + 和 - 图像拖到它们上面。否则,您可能需要寻找一个看起来像您想要的那样的自定义 NSButton。我相信已经有一些开源程序具有这些功能,但我无法立即记住它们。

I believe the button you are referencing is a custom button and probably a custom background image, although it could certainly be programmatically created. The problem with these types of buttons is that there have been several variants of that button which all look very similar but have slight differences.

The easiest way to create those buttons is to use the gradient buttons and drag the + and - images from Xcodes stock icons onto them. Otherwise you may want to look around for a custom NSButton that looks like what you want. I believe there have been some open sourced programs that have these, but I can't remember them right off the top of my head.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文