由于未捕获异常“CALayerInvalidGeometry”而终止应用程序,原因:“CALayer bounds包含NaN:[0 nan; 280 524] [英] Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 nan; 280 524]'

查看:821
本文介绍了由于未捕获异常“CALayerInvalidGeometry”而终止应用程序,原因:“CALayer bounds包含NaN:[0 nan; 280 524]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个书应用程序,用户可以在TextView上更改字体大小。

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.


由于未捕获的异常CALayerInvalidGeometry而终止应用程序,原因:CALayer bounds包含NaN:[0 nan; 280 524]'
* 的第一掷调用堆栈:
(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调用抛出异常

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

也无法重现此问题100%,因为这个问题偶尔会发生。

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


推荐答案

你得到一个无效的输出,导致 rect 有值 NaN (一个特殊的浮动值意味着不是有效浮点数,最常见的是由零除)。是否可能已将无效或无意义的 UITextRange 传递给 -firstRectForRange ?如果 UITextPosition * start 引用文本中的最后一个字符,然后创建另一个超出结束一个字符的文本位置,会发生什么?

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?

这篇关于由于未捕获异常“CALayerInvalidGeometry”而终止应用程序,原因:“CALayer bounds包含NaN:[0 nan; 280 524]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆