[英]Can an UIFont Object saved to NSUserDefaults
I am trying to allow iPhone app users to personalize an application by setting custom fonts. 我试图允许iPhone应用程序用户通过设置自定义字体来个性化应用程序。 I am wondering if I can save an UIFont Object to the NSUserDefaults.
我想知道是否可以将UIFont对象保存到NSUserDefaults中。 Would make things a lot easier to set the font and font size and then save the object.
设置字体和字体大小然后保存对象将使事情变得容易得多。 Code sample would be great too.
代码示例也很棒。
I would make a category to hide the complexity - you might start with something like this 我将创建一个类别来隐藏复杂性-您可以从类似这样的内容开始
.h 。H
// NSUserDefaults+CustomFontAdditions.h
@interface NSUserDefaults (CustomFontAdditions)
- (UIFont *)ps_fontForKey:(NSString *)fontKey;
- (void)ps_setFont:(UIFont *)font forKey:(NSString *)fontKey;
@end
.m .M
// NSUserDefaults+CustomFontAdditions.m
#import "NSUserDefaults+CustomFontAdditions.h"
NSString * const PSCustomFontsKey = @"PSCustomFontsKey";
NSString * const PSCustomFontKeyFontName = @"PSCustomFontKeyFontName";
NSString * const PSCustomFontKeyFontSize = @"PSCustomFontKeyFontSize";
@implementation NSUserDefaults (CustomFontAdditions)
- (UIFont *)ps_fontForKey:(NSString *)fontKey;
{
NSDictionary *fonts = [self dictionaryForKey:PSCustomFontsKey];
UIFont *font = nil;
if (fonts) {
NSDictionary *fontComponents = [fonts valueForKey:fontKey];
NSString *fontName = [fontComponents valueForKey:PSCustomFontKeyFontName];
CGFloat size = [[fontComponents valueForKey:PSCustomFontKeyFontSize] floatValue];
font = [UIFont fontWithName:fontName size:size];
}
return font;
}
- (void)ps_setFont:(UIFont *)font forKey:(NSString *)fontKey;
{
NSMutableDictionary *fonts = [[self dictionaryForKey:PSCustomFontsKey] mutableCopy];
if (!fonts) {
fonts = [[NSMutableDictionary alloc] initWithCapacity:1];
}
NSDictionary *fontComponents = [[NSDictionary alloc] initWithObjectsAndKeys:
font.fontName, PSCustomFontKeyFontName,
[NSNumber numberWithFloat:font.pointSize], PSCustomFontKeyFontSize, nil];
[fonts setValue:fontComponents forKey:fontKey];
[self setObject:fonts forKey:PSCustomFontsKey];
}
@end
Usage would look like 用法看起来像
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults ps_setFont:[UIFont systemFontOfSize:12.0f] forKey:@"font1"];
[standardUserDefaults ps_setFont:[UIFont fontWithName:@"Helvetica Neue" size:24.0f] forKey:@"font2"];
[standardUserDefaults synchronize];
NSLog(@"%@", [standardUserDefaults ps_fontForKey:@"font1"]);
NSLog(@"%@", [standardUserDefaults ps_fontForKey:@"font2"]);
Outputs: 输出:
#=> 2012-04-19 00:01:21.756 ma[1340:f803] <UICFFont: 0x68a18b0> font-family: "Helvetica"; font-weight: normal; font-style: normal; font-size: 12px
#=> 2012-04-19 00:01:21.765 ma[1340:f803] <UICFFont: 0x6b99670> font-family: "Helvetica Neue"; font-weight: normal; font-style: normal; font-size: 24px
These are the items that can be stored in NSUserDefaults: 这些是可以存储在NSUserDefaults中的项目:
NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary
NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary
You could do something like this: 您可以执行以下操作:
NSDictionary *fontInformation = [NSDictionary dictionaryWithObjectsAndKeys:
@"YOUR_FONTS_NAME", "fontName",
[NSNumber numberWithInt:14], @"fontSize", nil];
[[NSUserDefaults standardUserDefaults] setObject:fontInformation forKey:@"fontInfo"];
Then later you could grab the NSDictionary from NSUserDefaults and re-create the UIFont. 然后,您可以从NSUserDefaults中获取NSDictionary并重新创建UIFont。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.