我想创建一个属性字符串,然后将其存储在中NSUserDefaults,然后再次访问它,并将属性字符串分配给textView.attributedText。我该怎么办?提前致谢。
NSUserDefaults
textView.attributedText
您必须将转换NSMutableAttributedString为,NSData然后将其存储在中NSUserDefaults。
NSMutableAttributedString
NSData
// 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