Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 nan; 280 524]'
Asked Answered
A

2

13

I am developing a book app where users can change the font size on the TextView.

when users change the font size, the app save the current text position not to change that after users did changing font size.

It works fine in most cases, but sometimes, when users change the font size, the app gets this kind of error, and I still can't tell how to fix this.

Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 nan; 280 524]' * First throw call stack: (0x3231e3e7 0x3a019963 0x3231e307 0x33edb4d7 0x33edb30d 0x3419141f 0x3419b82f 0x342d1cf1 0x3414f80d 0xe7bf1 0xe607d 0xfad35 0x34218087 0x34218111 0x34218087 0x3421803b 0x34218015 0x342178cb 0x34217db9 0x341405f9 0x3412d8e1 0x3412d1ef 0x35e455f7 0x35e45227 0x322f33e7 0x322f338b 0x322f220f 0x3226523d 0x322650c9 0x35e4433b 0x341812b9 0xdf9f1 0xdf978) libc++abi.dylib: terminate called throwing an exception

I also can't reproduce this issue 100% since this issue happens occasionally.

This is the code. Does anybody have an idea to fix this? Thanks in advance.

- (UITextRange *)getCurrentTextRange:(UITextView *)textView {

    CGRect bounds               = textView.bounds;
    UITextPosition *start       = [textView characterRangeAtPoint:bounds.origin].start;
    UITextPosition *end         = [textView positionFromPosition:start offset:1];
    UITextRange *textRange      = [textView textRangeFromPosition:start toPosition:end];

    return textRange;
}

- (void)changeFontSize:(UIButton*)button {

    UITextRange *textRange  = [self getCurrentTextRange:_textView];
    int fontSize            = [[NSUserDefaults standardUserDefaults] integerForKey:@"fontSize"];

    //button.tag == 1 => decreaseFontSize
    if (button.tag == 1) {

        //set a minimum size
        if (fontSize <= [LSUniversalManager getFontSizeForMinimum]) {
            return;
        }

        fontSize--;

    //button.tag == 2 => increaseFontSize
    } else if (button.tag == 2) {        
        fontSize++;
    }

    _textView.font      = [UIFont systemFontOfSize:fontSize];
    [[NSUserDefaults standardUserDefaults] setInteger:fontSize forKey:@"fontSize"];

    //avoid shifting the scroll position after changing font size
    CGRect rect = [_textView firstRectForRange:textRange];
    [_textView setContentOffset:CGPointMake(0, rect.origin.y)];
}
Acoustic answered 7/4, 2013 at 15:26 Comment(0)
S
21

Somewhere along the line here, you are getting an invalid output that is causing rect to have the value NaN (a special float value meaning "not a valid float number", most commonly caused by division by zero). Is it possible that you have passed an invalid or nonsensical UITextRange to -firstRectForRange? What happens if UITextPosition *start references the last character in the text, when you then create another text position that is one character past the end?

Schizopod answered 7/4, 2013 at 15:41 Comment(3)
Thanks for you comments, I believe this cruch happens in the [_textView firstRectForRange:textRange] when textRange has invalid value in it as you mentioned. Do you think I can check if textRange is valid before I use firstRectForRange somehow? Sorry, I can't tell how to reproduce this situation. >>>What happens if UITextPosition *start references ... Maybe I should try with this. UITextPosition *end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds), CGRectGetMaxY(bounds))].end; at getCurrentTextRange method.Acoustic
If anyone else ever runs into it... I had a UIPopoverController presenting from a button on the edge of the screen and accidentally had the permitted direction putting it toward the outside. No room to present == this error.Garibold
In my case it was triggered by trying to display a UIWebView that had been created from an NSData object with invalid data, possibly from an empty file.Cominform
M
1

In my case, code was doing 0.0 / 0.0 resulting -nan (infinity) and used further for converting to double.

I added a guard condition to make sure values are > 0.0

Mellifluent answered 9/7, 2020 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.