16

I'm following an online tutorial to build a magazine type iOS application. I'm attempting to use NSAttributedStringKey but keep getting the error shown below. Any ideas?

This is the line of code that is causing the error:

let attrs = [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: font] as [NSAttributedStringKey : Any]
Johnm95
  • 169
  • 1
  • 1
  • 5

6 Answers6

33

The projects are probably in different versions of Swift.

In Swift 4, NSFontAttributeName has been replaced with NSAttributedStringKey.font.

as stated here NSFontAttributeName vs NSAttributedStringKey.font

Need to confirm whether it will work on versions less than ios11

Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
Tibin Thomas
  • 1,365
  • 2
  • 16
  • 25
  • The issue has nothing to do with the version of Swift. The API change is a result of changes in the OS version. The change came in iOS 11, macOS 10.13, tvOS 11.0, and watchOS 4.0. – rmaddy Jul 12 '18 at 20:31
20

You are trying to use an iOS 11 API on a pre-iOS 11 version (likely iOS 10). Surprised that you found a tutorial already using beta features!

In the meantime, try this.

let attrs = [NSForegroundColorAttributeName: color, NSFontAttributeName: font]

and that should work.

chrismanderson
  • 4,603
  • 5
  • 31
  • 47
7

The code you are trying to use is a new API added in iOS 11. Since you are using Xcode 8 you are not using iOS 11. So you need to use the current (non-beta) API.

let attrs = [NSForegroundColorAttributeName: color, NSFontAttributeName: font]
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thank you! that managed to get rid of the error. However, following the tutorial I am not receiving the output displayed. I'm assuming that the tutorial is possibly not able to work with my version? Tutorial - https://www.raywenderlich.com/153591/core-text-tutorial-ios-making-magazine-app – Johnm95 Jun 20 '17 at 00:44
  • You should find a tutorial that isn't based on the new beta and iOS 11. – rmaddy Jun 20 '17 at 00:45
6

Swift 4.1 and Xcode 9.3 changes Attributed key value.

let strName = "Hello Stackoverflow"
let string_to_color2 = "Hello"        
let attributedString1 = NSMutableAttributedString(string:strName)
let range2 = (strName as NSString).range(of: string_to_color2)       
attributedString1.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.red , range: range2)
lblTest.attributedText = attributedString1

Hello will be in red color.

Gautam Sareriya
  • 1,833
  • 19
  • 30
  • The issue has nothing to do with the version of Swift or Xcode. The API change is a result of changes in the OS version. The change came in iOS 11, macOS 10.13, tvOS 11.0, and watchOS 4.0. – rmaddy Jul 12 '18 at 20:31
3

This example works only iOS11.

import UIKit

class VC: UIViewController { 

    @IBOutlet weak var usernameTxt: UITextField!
    @IBOutlet weak var emailTxt: UITextField!
    @IBOutlet weak var passTxt: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
    }

    func setupView() {
        usernameTxt.attributedPlaceholder = NSAttributedString(string: "username", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])
        emailTxt.attributedPlaceholder = NSAttributedString(string: "email", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])
        passTxt.attributedPlaceholder = NSAttributedString(string: "password", attributes: [NSAttributedStringKey.foregroundColor: smackPurplePlaceholder])  
    }    
}
Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36
3

Swift 4.x

 // MARK: - Deal with the empty data set
// Add title for empty dataset
func title(forEmptyDataSet _: UIScrollView!) -> NSAttributedString! {
    let str = "Welcome"
    let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)]
    return NSAttributedString(string: str, attributes: attrs)
}

// Add description/subtitle on empty dataset
func description(forEmptyDataSet _: UIScrollView!) -> NSAttributedString! {
    let str = "Tap the button below to add your first grokkleglob."
    let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)]
    return NSAttributedString(string: str, attributes: attrs)
}

// Add your image
func image(forEmptyDataSet _: UIScrollView!) -> UIImage! {
    return UIImage(named: "MYIMAGE")
}

// Add your button
func buttonTitle(forEmptyDataSet _: UIScrollView!, for _: UIControlState) -> NSAttributedString! {
    let str = "Add Grokkleglob"
    let attrs = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.callout), NSAttributedStringKey.foregroundColor: UIColor.white]
    return NSAttributedString(string: str, attributes: attrs)
}

// Add action for button
func emptyDataSetDidTapButton(_: UIScrollView!) {
    let ac = UIAlertController(title: "Button tapped!", message: nil, preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "Hurray", style: .default, handler: nil))
    present(ac, animated: true, completion: nil)
}
Sunil Targe
  • 7,251
  • 5
  • 49
  • 80