可以将UIFont对象保存到NSUserDefaults

时间:2012-04-18 21:29:31

标签: iphone xcode nsuserdefaults uifont

我试图让iPhone应用程序用户通过设置自定义字体来个性化应用程序。我想知道我是否可以将UIFont对象保存到NSUserDefaults。可以使设置字体和字体大小更容易,然后保存对象。代码示例也很棒。

2 个答案:

答案 0 :(得分:3)

我会创建一个类别来隐藏复杂性 - 你可以从这样的东西开始

·H

// NSUserDefaults+CustomFontAdditions.h

@interface NSUserDefaults (CustomFontAdditions)

- (UIFont *)ps_fontForKey:(NSString *)fontKey;
- (void)ps_setFont:(UIFont *)font forKey:(NSString *)fontKey;

@end

的.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

用法看起来像

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"]);

输出:

#=> 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

答案 1 :(得分:2)

这些是可以存储在NSUserDefaults中的项目:

  

NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary

你可以这样做:

NSDictionary *fontInformation = [NSDictionary dictionaryWithObjectsAndKeys:
@"YOUR_FONTS_NAME", "fontName",
[NSNumber numberWithInt:14], @"fontSize", nil];

[[NSUserDefaults standardUserDefaults] setObject:fontInformation forKey:@"fontInfo"];

之后您可以从NSUserDefaults中获取NSDictionary并重新创建UIFont。