我正在使用NSUserDefaults存储以下信息:
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
[settings setObject:[NSArray arrayWithObjects:@"An Invitation.png",@"Apple Color Emoji ",[NSKeyedArchiver archivedDataWithRootObject:[UIColor redColor]], nil] forKey:@"An Invitation"];
我正在另一个类中检索相同的信息:
NSDictionary *dict = [settings dictionaryRepresentation];
for(id item in [dict allKeys]) {
NSLog(@"settings array is---- %@",[settings objectForKey:item]);
if([[dict objectForKey:item] isKindOfClass:[NSArray class]]) {
if([[settings objectForKey:@"CardImg"] isEqualToString:[[dict objectForKey:item] objectAtIndex:0]]) {
NSLog(@"received font is ---- %@",[[dict objectForKey:item] objectAtIndex:1]);
[self.txtView setFont:[UIFont fontWithName:[NSString stringWithFormat:@"%@",[[dict objectForKey:item] objectAtIndex:1]] size:20]];
NSLog(@"received font is ---- %@",[[self.txtView font] fontName]);
UIColor *txtcolor =(UIColor*) [ NSKeyedUnarchiver unarchiveObjectWithData:[[dict objectForKey:item] objectAtIndex:2]];
NSLog(@"received text color is ---- %@", [[dict objectForKey:item] objectAtIndex:2]);
[self.txtView setTextColor:txtcolor];
}
}
}
self.textView中的字体和颜色不会根据dict中的值进行更改。在打印[self.txtView.font fontName]
后,我收到Null,但在打印[[dict objectForKey:item] objectAtIndex:1]
时,我正在获得" Apple Color Emoji"。根据XIB属性检查器中的默认值设置字体和颜色。请帮帮我。
答案 0 :(得分:0)
字体为零,因为字体名称末尾有空格。并且颜色需要取消归档。从字体名称中删除空格并以这种方式检索已保存的设置:
NSArray *retrieved = [settings objectForKey:@"An Invitation"];
NSString *imageName = retrieved[0];
UIFont *font = [UIFont fontWithName:retrieved[1] size:12];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:retrieved[2]];