Change font size via UiBarButtonItem in UITextView, works just once

无人久伴 提交于 2019-12-11 15:28:15

问题


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];
}


来源:https://stackoverflow.com/questions/5421121/change-font-size-via-uibarbuttonitem-in-uitextview-works-just-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!