我看了看,看了看这个,当我终于发现它不起作用的时候。我正在尝试保存并加载用户选择的文本颜色。这是我的保存按钮:
-(IBAction)save123456 {
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:textview];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"myColor"];
}
这是我的负担:
-(IBAction)load123456 {
NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"myColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
}
如果有帮助,我的文本视图是textview。此外,我通过tuchupinside链接一切,所以让我知道我是否应该改变任何东西。
此外,如果有人知道如何保存用户选择的文本字体也会有所帮助。非常感谢!!
答案 0 :(得分:0)
好吧,你可以保存颜色的RGB。以及字体的字体名称。
因此,当您保存时,请将这些值存储为字体:font.fontName
,font.pointSize
和RGB的颜色。 Here您可以看到如何获取UIColor
对象的RGB。
这些都是NSString和float值,因此保存它们不会有任何问题。
- (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];
}
希望这会有所帮助。 顺便说一句:如果您觉得答案有用,请将其标记为正确。
干杯,
乔治