Change font size via UiBarButtonItem in UITextView

2019-09-03 19:17发布

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条回答
爷、活的狠高调
2楼-- · 2019-09-03 20:05

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];
}
查看更多
登录 后发表回答