How to use store and use an NSMutableAttributedString in NSUserDefaults

后端 未结 1 1821
轮回少年
轮回少年 2020-12-17 04:01

I want to create an attributed string, then store it in NSUserDefaults, and then access it again and assign the attributed string to textView.attributedTe

相关标签:
1条回答
  • 2020-12-17 04:36

    You have to convert your NSMutableAttributedString into NSData then you store it in NSUserDefaults.

        // Convert into NSData
        let data = NSKeyedArchiver.archivedDataWithRootObject(distanceMutableAttributedString)
        NSUserDefaults.standardUserDefaults().setObject(data, forKey: "yourStringIntoData")
    
        // Convert your NSData to NSMutableAttributedString
        let yourStringInData = NSUserDefaults.standardUserDefaults().objectForKey("yourStringIntoData") as? NSData
        let newStr = NSKeyedUnarchiver.unarchiveObjectWithData(yourStringInData!) as? NSMutableAttributedString
    
       // Assign it to your textView
        textView.attributedText = newStr
    
    0 讨论(0)
提交回复
热议问题