我試圖讓iPhone應用程序用戶通過設置自定義字體來個性化應用程序。我想知道是否可以將UIFont對象保存到NSUserDefaults。將事情更容易設置字體和字體大小,然後保存該對象。代碼示例也會很好。可以將UIFont對象保存到NSUserDefaults
0
A
回答
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
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。
相關問題
- 1. 將UIColor,UIFont和NSDictionary保存到NSUserDefaults
- 2. NSUserDefaults是否可以使用自定義對象來保存NSArray?
- 3. 保存UIFont?以UserDefault Swift 3
- 4. 將AnyObject保存到NSUserdefaults
- 5. 將數組保存到NSUserDefaults
- 6. 將自定義對象保存到NSUserDefaults中
- 7. 在Monotouch中,是否可以將通用列表保存到NSUserDefaults?
- 8. 使用NSUserDefaults保存MPMediaItemCollection對象
- 9. NSUserDefaults沒有保存完整對象
- 10. 如何將對象添加到NSMutableArray中以存儲在NSUserDefaults中?
- 11. 我在哪裏可以保存NSUserDefaults
- 12. 是否可以在NSUserDefaults中保存NSManagedObject?
- 13. 保存NSTimer到NSUserDefaults
- 14. 將對象保存到Firebase
- 15. NSUserDefaults不保存可變的對象數組
- 16. 保存自定義對象以在NSUserDefaults的斯威夫特
- 17. 添加到以前保存的NSUserDefaults
- 18. 我可以將File對象保存到localstorage中嗎?
- 19. 將細胞附件保存到NSUserDefaults
- 20. 將聯繫人列表保存到NSUserDefaults
- 21. 如何將tableView數據保存到NSUserDefaults?
- 22. 將字符串保存到NSUserDefaults?
- 23. 如何將數組保存到NSUserDefaults?
- 24. 將NSArray保存到NSUserDefaults不起作用
- 25. 如何將MPMediaItemCollection保存到NSUSerDefaults?
- 26. 將NSMutableDictionary保存爲NSUserDefaults
- 27. 將UIImage保存爲NSUserDefaults
- 28. iOS NSDictionary保存到NSUserDefaults不保存值
- 29. 保存的NSMutableArray NSUserDefaults的到
- 30. 列表保存到NSUserDefaults的
謝謝保羅...我實施了你的解決方案,它的功能就像一個魅力。欣賞詳細的代碼示例 – 239revilo 2012-04-19 21:36:23