2012-04-18 82 views
0

我試圖讓iPhone應用程序用戶通過設置自定義字體來個性化應用程序。我想知道是否可以將UIFont對象保存到NSUserDefaults。將事情更容易設置字體和字體大小,然後保存該對象。代碼示例也會很好。可以將UIFont對象保存到NSUserDefaults

回答

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 
+0

謝謝保羅...我實施了你的解決方案,它的功能就像一個魅力。欣賞詳細的代碼示例 – 239revilo 2012-04-19 21:36:23

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。

相關問題