【问题标题】:How can I set the text color of an NSButton via Interface Builder?如何通过 Interface Builder 设置 NSButton 的文本颜色?
【发布时间】:2017-12-27 21:30:08
【问题描述】:

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

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

【问题讨论】:

    标签: xcode interface-builder nsbutton


    【解决方案1】:

    我不知道为什么 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)
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个解决方案,我希望你会得到:)

      NSFont *txtFont = button.font;
      NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
      [style setAlignment:button.alignment];
      NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSColor whiteColor], NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, txtFont, NSFontAttributeName, nil];
      NSAttributedString *attrString = [[NSAttributedString alloc]
                                            initWithString:button.title attributes:attrsDictionary];
      [button setAttributedTitle:attrString];
      

      【讨论】:

      • @quemeful 不可以设置,但是可以通过运行时属性设置。
      【解决方案3】:

      如果您喜欢“加入扩展程序并查看它是否粘住”的方法,也可以将此扩展程序添加到您的代码中。

      extension NSButton {
      
        @IBInspectable open var textColor: NSColor? {
          get {
            return self.attributedTitle.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? NSColor
          }
          set {
            var attributes = self.attributedTitle.attributes(at: 0, effectiveRange: nil)
            attributes[.foregroundColor] = newValue ?? NSColor.black
            self.attributedTitle = NSMutableAttributedString(string: self.title,
                                                             attributes: attributes)
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        编辑:误读的问题。以下是如何更改 iOS 应用上按钮的文本。

        澄清一下,这对你不起作用?

        • 添加按钮
        • 点击它并转到属性检查器
        • 在“文本颜色”字段中更改颜色

        【讨论】:

        • 问题是关于 NSButton(对于 OS X 应用程序,而不是 iPhone)。属性检查器中没有“文本颜色”字段(至少在 Xcode 6.1.1 中没有)。
        猜你喜欢
        • 2014-08-01
        • 1970-01-01
        • 2018-02-11
        • 2011-09-16
        • 2013-01-12
        • 1970-01-01
        • 1970-01-01
        • 2011-02-22
        • 2017-03-27
        相关资源
        最近更新 更多