【问题标题】:Swift Mac OSX NSButton title colorSwift Mac OSX NSButton 标题颜色
【发布时间】:2015-01-10 11:39:21
【问题描述】:

我想知道如何在 swift 中将标题颜色更改为 NSButton,我在 Objective-c 中看到了很多示例,但我认为在 swift 中实现是不同的,谁能给我一个示例?

【问题讨论】:

    标签: macos swift xcode6 nsbutton


    【解决方案1】:

    viewDidLoad 或其他地方试试这个。

    在 Swift 3 中:

        let pstyle = NSMutableParagraphStyle()
        pstyle.alignment = .center
    
        button.attributedTitle = NSAttributedString(string: "Title", attributes: [ NSForegroundColorAttributeName : NSColor.red, NSParagraphStyleAttributeName : pstyle ])
    

    【讨论】:

    • 在 Swift 2 中,您需要将 .CenterTextAlignment 更改为 .Center
    • Swift 3 版本:let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center button.attributedTitle = NSAttributedString(string: "Title", attributes: [NSForegroundColorAttributeName : NSColor.red, NSParagraphStyleAttributeName : paragraphStyle])
    • 垂直对齐呢?
    【解决方案2】:

    斯威夫特 4

    我已将此作为附加答案添加,因为它仅更改请求的属性而不会覆盖或添加其他属性。

    if let mutableAttributedTitle = button.attributedTitle.mutableCopy() as? NSMutableAttributedString {
        mutableAttributedTitle.addAttribute(.foregroundColor, value: NSColor.white, range: NSRange(location: 0, length: mutableAttributedTitle.length))
        button.attributedTitle = mutableAttributedTitle
    }
    

    【讨论】:

      【解决方案3】:

      在 Swift4 中

      button.attributedTitle = NSMutableAttributedString(string: "Hello World", attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.paragraphStyle: style, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 18)])
      

      【讨论】:

      • Xcode 9 with swift 4 还没有出来,也不是通用汽车,所以虽然这可能不会改变,但我认为你在这里过火了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 2020-07-14
      • 2016-09-13
      • 1970-01-01
      • 2017-05-14
      相关资源
      最近更新 更多