更改 NSProgressIndicator 的颜色?

问题描述 投票:0回答:8

更改 NSProgressIndicator 颜色的最佳方法是什么,有没有比直接子类化然后自己绘制整个组件更简单的方法?

基本上我想做的是拥有一个类似的组件,但能够更改栏的颜色。

我尝试用谷歌搜索这个问题,但所有问题都已经过时了,并且与我正在开发的 10.10 OS X 版本无关。还检查了 cocoa 控件,只找到了 1 个适用于过时 OS X 版本的组件。

objective-c macos cocoa nsprogressindicator
8个回答
17
投票

您可以直接在 Interface Builder 中使用 Quartz 滤镜(例如色调调整)。这比预期的效果更好。

NSProgressIndicator

它位于效果检查器中。在“内容过滤器”下,您可以添加“色调调整”


1
投票

使用“CIFalseColor”滤镜获得白色等。

let colorFilter = CIFilter(name: "CIFalseColor")!
colorFilter.setDefaults()
colorFilter.setValue(color1, forKey: "inputColor0")
colorFilter.setValue(color2, forKey: "inputColor1")
proggressBar?.contentFilters = [colorFilter]

1
投票

我试图改变大多数答案中看到的色调,但我遇到了很多问题以获得我想要的正确的特定颜色。

对我有用的,并且似乎是获得特定颜色的最直接方法,是使用 CIColorMonochrome 过滤器,您可以在其中设置您想要的任何 RGB 颜色:

let myCustomColor = CIColor(red: 10, green: 10, blue: 10)
if let colorMonochrome = CIFilter(name: "CIColorMonochrome", parameters: [kCIInputColorKey: myCustomColor]) {
    progressIndicator.contentFilters.append(colorMonochrome)
}

0
投票

要更改

NSProgressIndicator
的颜色,请使用 setControlTint: 方法。如果您想设置自定义颜色,则必须手动绘制此类控件。但是,您应该使用系统颜色来保持这种控制在整个系统中保持一致。


0
投票

对于 Swift,方法名称为

controlTint

progressIndicator = NSProgressIndicator(frame: .......
progressIndicator.controlTint = .blueControlTint

0
投票

您可以使用 proggressBar.appearance = NSAppearance(named: .vibrantLight) // 这是“黑色”指示器的 light 或 vibrantDark


0
投票

根据 Paxos 在接口构建器上的回答,这就是我能够以编程方式完成此操作的方式:

let progress = NSProgressIndicator()
progress.contentFilters = [CIFilter(name: "CIHueAdjust", parameters: ["inputAngle": 4])!] 

这将使栏变成绿色。我通过查看 Main.storyboard 差异得到了这个:

<progressIndicator maxValue="100" doubleValue="50" style="bar" translatesAutoresizingMaskIntoConstraints="NO" id="NIr-vo-obX">
  <rect key="frame" x="3" y="22" width="210" height="20"/>
+ <contentFilters>
+  <ciFilter name="CIHueAdjust">
+   <configuration>
+    <real key="inputAngle" value="4"/>
+    <null key="inputImage"/>
+   </configuration>
+  </ciFilter>
+ </contentFilters>
</progressIndicator>

0
投票

根据KamyFC的答案,我发现名为“CIFalseColor”的过滤器需要

CIColor

这是 Objective-C 解决方案,可以使进度条变成任何颜色

NSColor
,在本例中为橙色。 不要忘记将
@import Quartz;
添加到您的文件中。

// Create color:
CIColor *color = [[CIColor alloc] initWithColor:[NSColor orangeColor]];

// Create filter:
CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                             withInputParameters:@{@"inputColor0" : color,
                                                   @"inputColor1" : color}];

// Assign to bar:
_progressBar.contentFilters = @[colorFilter];
© www.soinside.com 2019 - 2024. All rights reserved.