2

In a macOS project using Swift 3.2, I'm trying to set the foreground color of a UITextView.

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

The error I get is this:

Type 'NSAttributedStringKey' (aka 'NSString') has no member 'foregroundColor'

The same code works fine in a Swift 4.0 project.

I'm trying to modify the code based on some answers I found for iOS Swift 4 Conversion error - NSAttributedStringKey: Any but I continue to get errors. Is there a way to fix this without to update the project to Swift 4?

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Cue
  • 2,952
  • 3
  • 33
  • 54

1 Answers1

5

Swift 3 doesn't use the new Swift 4 NSAttributeStringKey values.

Use NSForegroundColorAttributeName for the foreground color:

let placeHolderTitleString: NSAttributedString =
    NSAttributedString(string: "Enter text here", attributes:
    [NSForegroundColorAttributeName : NSColor.gray])
rmaddy
  • 314,917
  • 42
  • 532
  • 579