1

I looked and looked and looked for this and when i finally find something it does not work. I am trying to save and load text color that the user selects. Here is my save button:

-(IBAction)save123456 {
    NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:textview];
    [[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];
}

And here is my load:

-(IBAction)load123456 {
    NSData  *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
    UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
}

My text view is textview if that helps. Also i am linking everything via tuchupinside so let me know if i should change anything.

Also if anyone knows how to save text font that the user selects would also be helpful. Thanks so much!!

CodaFi
  • 43,043
  • 8
  • 107
  • 153
Nicholas
  • 69
  • 1
  • 9

1 Answers1

0

Well , you could save the RGB of the color. And the font-name of the font. So when you're saving , store these values for font: font.fontName , font.pointSize and RGB of the color. Here you can see how to get the RGB of a UIColor object. These are all NSString and float values so you shouldn't have any problem in saving them.

- (NSString *) pahtForFile:(NSString*) filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:filename];
}


- (void) save
{
//get RGB and fontName , fontSize like I explained above

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:fontName forKey:@"fontName"];
[dictionary setObject:[NSNumber numberWithFloat:fontSize]   forKey:@"fontSize"];
[dictionary setObject:[NSNumber numberWithFloat:red]    forKey:@"red"];
[dictionary setObject:[NSNumber numberWithFloat:green]  forKey:@"green"];
[dictionary setObject:[NSNumber numberWithFloat:blue]   forKey:@"blue"];

NSString *filepath = [self pathForFile:@"save.plist"];
[dictionary writeToFile:filepath atomically:TRUE];
}

- (void) load 
{
float red,green,blue,fontSize;
NSString *fontName;

NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[self pahtForFile:@"save.plist"]];
red = [[dictionary objectForKey:@"red"] floatValue];
green = [[dictionary objectForKey:@"green"] floatValue];
blue = [[dictionary objectForKey:@"blue"] floatValue];

fontSize = [[dictionary objectForKey:@"fontSize"] floatValue];
fontName = [dictionary objectForKey:@"fontName"];

//now rebuild color and font like this:
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1];
UIFont *font = [UIFont fontWithName:fontName size:fontSize];
}

Hope this helps. BTW: If you find the answer useful , mark it as correct.

Cheers,

George

Community
  • 1
  • 1
George
  • 4,029
  • 1
  • 23
  • 31
  • Okay thank you soooo much. I am really new to Xcode and i have no idea what i am doing. You think there is anyway you could come up with some demo code? That would help soooo much. Thanks – Nicholas Jul 05 '12 at 07:14
  • See my edited answer. That will save your values in a file. The advantage for a beginner on iOS is that you can run the app on the simulator and check the actual file to see if it saved the info correctly. To fint the path to the file , just log what `pathForFile` returns. – George Jul 05 '12 at 07:51
  • Okay code looks good only have a few problems. Except the dictionary's in the save under the NSMUltiableDictionary have errors. They are all say Use Of Undeclared Identifier...Fontname,or the other colors. Also the NSString in the save. That error is no visible @interface declares the selector 'pathForFile' – Nicholas Jul 05 '12 at 17:07
  • Well, let's say you want to save / load in a class called MyClass. Declare in MyClass.h the pathForFile method. After that in your MyClass.m implement everything you need + save/load methods above. In your save method , before pasting the code I wrote , get the RGB of the color as shown in the link provided in my answer. For the font , it's enough to say font.fontName and font.pointSize. But for the RGB , you'll have to look at that link . We are giving answers here on SO and trying to help you. We are not going to make entire projects and upload them. – George Jul 05 '12 at 21:34