保存浮点值(UIFontSize)由NSUserDefaults

时间:2013-06-15 14:52:24

标签: size nsuserdefaults uifont

如何通过NSUserDefaults保存浮点值(UIFontSize)?

(void)pinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer{NSLog(@"*** Pinch: Scale: %f Velocity: %f", gestureRecognizer.scale, gestureRecognizer.velocity);

UIFont *font = self.bioTextView.font;
CGFloat pointSize = font.pointSize;
NSString *fontName = font.fontName;

pointSize = ((gestureRecognizer.velocity > 0) ? 1 : -1) * 1 + pointSize;

if (pointSize < 13) pointSize = 13;
if (pointSize > 26) pointSize = 26;

self.bioTextView.font = [UIFont fontWithName:fontName size:pointSize];

1 个答案:

答案 0 :(得分:0)

问题解决了:

- (void)viewDidLoad
{
[super viewDidLoad];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];

// And add it to your text view.
[self.bioTextView addGestureRecognizer:pinchGesture];

UIFont *font = self.bioTextView.font;
NSString *fontName = font.fontName;

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs objectForKey:@"floatKey"]==nil) {
    self.bioTextView.font = [UIFont fontWithName:fontName size:18];
} else {
    float myFloat = [prefs floatForKey:@"floatKey"];
    self.bioTextView.font = [UIFont fontWithName:fontName size:myFloat];
}
}


- (void)pinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{
NSLog(@"*** Pinch: Scale: %f Velocity: %f", gestureRecognizer.scale, gestureRecognizer.velocity);

UIFont *font = self.bioTextView.font;
CGFloat pointSize = font.pointSize;
NSString *fontName = font.fontName;

pointSize = ((gestureRecognizer.velocity > 0) ? 1 : -1) * 1 + pointSize;

if (pointSize < 13) pointSize = 13;
if (pointSize > 26) pointSize = 26;

self.bioTextView.font = [UIFont fontWithName:fontName size:pointSize];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:pointSize forKey:@"floatKey"];
[prefs synchronize];
}