How to allow app users to set font, size, color and alignment an application by custom setting? I am wondering if I can save an UIFont to the NSUserDefaults. How can I get around this issue?
Asked
Active
Viewed 582 times
1
-
1See [this answer](http://stackoverflow.com/questions/1275662/saving-uicolor-to-and-loading-from-nsuserdefaults) for `UIColor`. For the `UIFont` now (since it doesn't adopt the `NSCoding` protocol, you could just keep a string with the font name or a dictionary with the required parameters and pass them to a method that would return a `UIFont` based on those upon retrieval from the defaults. – Alladinian Jun 02 '13 at 11:47
-
Have you looked into http://www.cocoacontrols.com There are ton of sample projects that let users choose font colors and change font text or sizes – Sam B Jun 02 '13 at 12:00
-
Yes. Have a exellent example https://github.com/chrismiles/CMTextStylePicker , but i need to save by NSUserDefaults – German Jun 02 '13 at 12:41
1 Answers
1
You can create a NSDictionary
holding:
Font?
Its PostScript name.
Size?
NSNumber
Color?
NSData
(see below)
Alignment?
NSNumber
NSUserDefaults
can save NSData
. To serialize:
NSData * data = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults sharedUserDefaults] setObject:data forKey:MONDefaultsKey];
then use NSKeyedUnarchiver
to deserialize the NSData
representation.
You might also consider saving it as a serialized NSAttributedString
if there are associated string(s) -- but that is NSUserDefaults
abuse in most cases.

justin
- 104,054
- 14
- 179
- 226