NSAttributedStringKey giving an unresolved identifier error

后端 未结 6 1756
暖寄归人
暖寄归人 2020-12-30 04:15

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?

相关标签:
6条回答
  • 2020-12-30 04:38

    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)
    }
    
    0 讨论(0)
  • 2020-12-30 04:40

    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.

    0 讨论(0)
  • 2020-12-30 04:43

    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])  
        }    
    }
    
    0 讨论(0)
  • 2020-12-30 04:44

    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

    0 讨论(0)
  • 2020-12-30 04:44

    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.

    0 讨论(0)
  • 2020-12-30 04:46

    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]
    
    0 讨论(0)
提交回复
热议问题