【问题标题】:How to change color text of Button with AppleScript?如何使用 AppleScript 更改 Button 的颜色文本?
【发布时间】:2021-04-23 06:18:35
【问题描述】:

有没有办法在选择按钮时更改按钮文本的颜色?

我尝试了类似的方法:

property myButton : missing value

myButton's setTintColor:(current application's NSColor's cyanColor)

但我收到以下错误:

-[NSButton setTintColor:]:无法识别的选择器发送到实例 0x7fc865f2d000(错误 -10000)

【问题讨论】:

  • 什么样的按钮?请注意,tintColor 来自 iOS。
  • 嵌入式按钮或按钮。
  • 按下时没有备用标题,只有具有状态的标题,例如复选框。您只是想设置标题颜色吗?
  • 是的,我只是想定义标题颜色。

标签: applescript applescript-objc


【解决方案1】:

要将颜色等属性应用于按钮标题,您可以使用NSAttributedString,例如:

    set buttonTitle to myButton's title -- get current title
    set attrString to current application's NSMutableAttributedString's alloc's initWithString:buttonTitle
    attrString's addAttribute:(current application's NSForegroundColorAttributeName) value:(current application's NSColor's cyanColor) range:{0, buttonTitle's |length|}
    set myButton's attributedTitle to attrString

【讨论】:

  • 谢谢,它就像一个魅力!我可以使用 NSHomeTemplate 更改 NSImageView 的颜色吗?
  • 图像视图不是文本,但如果您使用 Mojave 或更高版本,它确实具有 contentTintColor 属性。
  • 同样的错误:-[NSImageView setcontentTintColor:]: unrecognized selector sent to instance 0x7fb6dec71c50 (error -10000)
  • 方法名称区分大小写——setter方法在添加set时大写,所以名称为setContentTintColor:。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2019-07-31
  • 2017-12-02
  • 2021-01-25
  • 2011-01-01
  • 1970-01-01
相关资源
最近更新 更多