简体   繁体   中英

UITextView font size not updated

I have a UITextView inside a UIViewController which contains some buttons: two of them are used to zoom the text contained in the UITextView . defaultFontSize is initialized on viewDidLoad()

Those buttons have a IBAction linked to them...

- (IBAction)zoomIn:(id)sender {
    if(defaultFontSize < 60) {
        defaultFontSize = defaultFontSize + 2;
        [theContent setFont:[UIFont systemFontOfSize:defaultFontSize]];
    }
}

/* ... */

- (IBAction)zoomOut:(id)sender {
    if(defaultFontSize > 14) {
        defaultFontSize = defaultFontSize - 2;
        [theContent setFont:[UIFont systemFontOfSize:defaultFontSize]];            
    }
}

When I first show the UIViewController and tap the zoomIn or zoomOut buttons the text size in theContent is not changing.

To be more precise: the methods are called (I've put some logs), but the UITextView font size is not updated.

BTW it's changing if I tap other buttons (the buttond tapped are changing screen opacity, so I think that this forces a screen refresh) taking all the taps I made before (ie if I tapped zoomIn 2 times I got a +4 in font size) and then start works normally.

Questions: How can I (if possible) force whenever I want a "refresh" of the UITextView ? Why my buttons are not working when I start showing the UIViewController , but then they work after a "screen refresh"?

I will be glad if you can help :-)

Call setNeedsLayout on the UITextView right after you change the font size. Certain methods must be invalidating the layout (like changing the opacity) which makes a lot of sense. Changing the font characteristics isn't going to invalidate the layout - you have to tell the OS that is needs to be laid out again.

- (IBAction)zoomIn:(id)sender {
    if(defaultFontSize < 60) {
        defaultFontSize = defaultFontSize + 2;
        [theContent setFont:[UIFont systemFontOfSize:defaultFontSize]];
        [theContent setNeedsLayout];
    }
}

- (IBAction)zoomOut:(id)sender {
    if(defaultFontSize > 14) {
        defaultFontSize = defaultFontSize - 2;
        [theContent setFont:[UIFont systemFontOfSize:defaultFontSize]];           
        [theContent setNeedsLayout]; 
    }
}

The stupid solutions did the trick... I saved the content in a temporary var and reinserted it... Not so proud of it, but it's working....

- (IBAction)zoomIn:(id)sender {

    if(defaultFontSize < 60) {
        NSString *tmpString;
        tmpString = theContent.text;
        defaultFontSize = defaultFontSize + 2.0;
        [theContent setFont:[UIFont systemFontOfSize:defaultFontSize]];
        [theContent setText:@""];
        [theContent setText:tmpString];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM