xcode - 如何通过 Interface Builder 设置 NSButton 的文本颜色?

标签 xcode interface-builder nsbutton

关于如何以编程方式设置文本颜色有几个问题。这一切都很好,但也必须有一种方法可以通过 Interface Builder 做到这一点。

“显示字体”框可用于更改按钮文本的大小,但 Xcode 会忽略使用该小部件所做的任何颜色更改,并且 NSButton 的属性检查器没有颜色选择器...

最佳答案

我不知道为什么 NSButton 仍然缺少这个。但这是 Swift 4 中的替换类:

import Cocoa

class TextButton: NSButton {
    @IBInspectable open var textColor: NSColor = NSColor.black
    @IBInspectable open var textSize: CGFloat = 10

    public override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }

    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override func awakeFromNib() {
        let titleParagraphStyle = NSMutableParagraphStyle()
        titleParagraphStyle.alignment = alignment

        let attributes: [NSAttributedStringKey : Any] = [.foregroundColor: textColor, .font: NSFont.systemFont(ofSize: textSize), .paragraphStyle: titleParagraphStyle]
        self.attributedTitle = NSMutableAttributedString(string: self.title, attributes: attributes)
    }
}

enter image description here

enter image description here

关于xcode - 如何通过 Interface Builder 设置 NSButton 的文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426753/

相关文章:

javascript - 如何检查 WKWebView 是否加载了 HTML 字符串

ios - 将被拒绝的应用程序的新版本上传到 App Store

objective-c - 将 C 函数设置为 NSButton 的选择器不会产生任何结果

cocoa - 当堆栈 View 大小调整时,如何使 NSButton 与 NSStackView 分离?

xcode - XCode 中的语法感知选择?

ios - 空指针的 Xcode 静态分析

iphone - subview 中的 TabBarController

ios - 自动布局 : Responsive autoshrinking multiple labels

Cocoa,使用集合运算符和多对多关系属性进行绑定(bind)

objective-c - NSButton 的 MouseDown 事件?