【问题标题】:Error setting 'foregroundColor' in macOS在 macOS 中设置“foregroundColor”时出错
【发布时间】:2018-01-05 21:43:03
【问题描述】:

在使用 Swift 3.2 的 macOS 项目中,我正在尝试设置 UITextView 的前景色。

let placeHolderTitleString: NSAttributedString = NSAttributedString(string: "Enter text here", attributes: [NSAttributedStringKey.foregroundColor : NSColor.gray]) // error

我得到的错误是这样的:

类型“NSAttributedStringKey”(又名“NSString”)没有成员 '前景颜色'

相同的代码在 Swift 4.0 项目中运行良好。

我正在尝试根据我为 iOS Swift 4 Conversion error - NSAttributedStringKey: Any 找到的一些答案修改代码,但我仍然遇到错误。有没有办法在不将项目更新到 Swift 4 的情况下解决这个问题?

【问题讨论】:

    标签: swift macos nsattributedstring nsattributedstringkey


    【解决方案1】:

    Swift 3 不使用新的 Swift 4 NSAttributeStringKey 值。

    使用NSForegroundColorAttributeName 作为前景色:

    let placeHolderTitleString: NSAttributedString =
        NSAttributedString(string: "Enter text here", attributes:
        [NSForegroundColorAttributeName : NSColor.gray])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 2016-09-18
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多