Change font size via UiBarButtonItem in UITextView

2019-09-03 19:59发布

问题:

I have UiTextView where I want change font size (increase), here code

-(void)biggerFont:(UIBarButtonItem *) item {
    CGFloat i = [UIFont systemFontSize];
    textView.font = [UIFont fontWithName:@"Tahome" size:i];
    i+=2;    
}

But it works just once, if you push once and after that again - fon size won't change. Help me please

回答1:

It's easy you are not saving the state of i persistently. Everytime you set i to the same value. After the method has been run through i is discarded cause it only exists in the methods scope.

Change i to a property something like

CGFloat myFontSize;

@property(nonatomic) CGFloat myFontSize;

For example in the viewWillLoad you set the default value

self.myFontSize = [UIFont systemFontSize];

And your method changes to

-(void)biggerFont:(UIBarButtonItem *) item 
{
    myFontSize += 2;
    textView.font = [UIFont fontWithName:@"Tahome" size:myFontSize];
}