How to use store and use an NSMutableAttributedString in NSUserDefaults
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
Save & Format String using NSUserDefaults
Solution found.
Save the new text for example here
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let data = NSKeyedArchiver.archivedDataWithRootObject(textView.attributedText)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "sawedString")
textView.resignFirstResponder()
}
Upload saved text when opening
override func viewDidLoad() {
super.viewDidLoad()
if let myStringData = NSUserDefaults.standardUserDefaults().objectForKey("sawedString") as? NSData {
let savedString = NSKeyedUnarchiver.unarchiveObjectWithData(myStringData) as? NSAttributedString
textView.attributedText = savedString
}
}
Use the NSAttributedString instead of the NSMutableAttributedString!
Saving rich text in NSUserDefaults
SaveFBCover1
is getting an NSString from Cover1.stringValue
, not an NSAttributedString
, because the . It looks like your code saves that string properly, and you're probably getting the string back when you load it again, but you won't get attributes because you didn't start with an attributed string, nor did you get the attributes of the text in the text view and save them separately.
You can get an attributed string by using
SaveFBCover1 = Cover1.attributedStringValue;
However, you're going to have to do a little more work to save and load the string (not much, though). NSAttributedString
isn't one of the types that you can save directly in NSUserDefaults
, so you'll need to serialize it into an instance of NSData
first. You can do that quite easily using an keyed archiver. Read about Keyed Archives in the Archives and Serializations Programming Guide.
Simple way to store NSMutableAttributedString in CoreData
I started using CoreText
when iOS5 was out, and thus used the Core Foundation values as attributes. However I now realize that since iOS6 came out, I can now use NSForegroundColorAttributeName
, NSParagraphStyleAttributeName
, NSFontAttributeName
, etc. in the attributes dictionary, and those keys are accompanied by objects like UIColor
, NSMutableParagraphStyle
, and UIFont
which can be archived.
Adding NSAttributedString to CloudKit
As seen in the documentation for CKRecord
, you can only store a limited set of data types. Given the possible types, your best option would be to convert the NSAttributedString
to NSData
. This can be done using an NSKeyedArchiver
. Then when you read the data back from CloudKit, you can convert the NSData
back to the original NSAttributedString
using NSKeyedUnarchiver
.
Please see https://stackoverflow.com/a/36940864/1226963 for an answer that shows how to go back and forth between NSAttributedString
and NSData
. (actually that answer uses NSMutableAttributedString
). And of course you would put/get the NSData
into/from a CKRecord
instead of NSUserDefaults
.
How to store multiple NSMutableDictionarys in NSUserDefaults
This error is due to calling mutable functions in immutable objects
Userdefaults always return a immutable objects.
so make mutable when u get objects from userdefaults.
NSMutableArray *arrayofdics = [[[NSUserDefaults standardUserDefaults] objectForKey:@"arrayofdics"] mutableCopy];
Related Topics
Getting Location for an iOS App When It Is in the Background and Even Killed
Mpmediaitemartwork Is Null While Cover Is Available in Itunes
Uitableview: Nested Section Headers
I Just Want to Open Ms Outlook App and See Mailto Screen Using Url Scheme at iOS
How to Change Device Orientation Programmatically in Swift
How to Determine the Correct Altitude for an Mkmapcamera Focusing on an Mkpolygon
Cannot Get My Location to Show Up in Google Maps in iOS 8
Cannot Assign Class Instance to Its Protocol Type
Synchronizing Read/Write Access to an Instance Variable for High Performance in iOS
Delphi/Firemonkey Change iOS Screen Rotation at Runtime
Not Working Transitions in Spritekit
How to Decode/Convert a Base64 String to Nsdata
Table View Didselectrowatindexpath, Only Works After Selecting Second Click
Pfobject' Does Not Have a Member Named 'Subscript'
Mapkit Annotations Disappearing
Dismiss and Present View Controller in Swift
Storing [Nsnull Null] Values in Nsuserdefaults, from JSON Serialization, Causes Unwanted Exceptions