0

如何通过 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];
4

1 回答 1

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];
}
于 2013-06-15T15:57:03.697 回答