2013-04-07 52 views
7

我正在開發一個圖書應用程序,用戶可以在TextView上更改字體大小。終止應用程序由於未捕獲的異常「CALayerInvalidGeometry」,原因:'CALayer界限包含NaN:[0 nan; 280 524]'

當用戶更改字體大小時,應用程序保存當前文本位置不會在用戶更改字體大小後更改。

它在大多數情況下工作正常,但有時,當用戶更改字體大小時,應用程序會得到這種錯誤,並且我仍然無法知道如何解決此問題。

由於未捕獲的異常「CALayerInvalidGeometry」導致應用終止,原因:'CALayer界限包含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:終止調用拋出異常

我也不能100%重現此問題,因爲偶爾會發生此問題。

這是代碼。有人有想法解決這個問題嗎?提前致謝。

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

回答

13

沿此線的某處你得到,是造成rect具有價值NaN無效輸出(特殊浮點值的意思是「不是一個有效的浮點數」,最常見原因被零除) 。是否有可能通過了無效或無意義的UITextRange-firstRectForRange?如果UITextPosition *start引用了文本中的最後一個字符,那麼當您創建另一個文本位置是最後一個字符時會發生什麼?

+0

感謝您的評論,我相信當textRange在其中有無效值時,這個cruch發生在[_textView firstRectForRange:textRange]中,正如您所提到的。 你是否認爲我可以檢查textRange是否有效,然後以某種方式使用firstRectForRange? 對不起,我不知道如何重現這種情況。 >>>如果UITextPosition *開始引用會發生什麼...... 也許我應該試試這個。 UITextPosition * end = [textView characterRangeAtPoint:CGPointMake(CGRectGetMaxX(bounds),CGRectGetMaxY(bounds))]。end;在getCurrentTextRange方法。 – 2013-04-08 03:46:47

+1

如果有其他人遇到過它......我有一個'UIPopoverController',通過屏幕邊緣的一個按鈕呈現,並且意外地將允許的方向放到了外面。沒有空間來呈現==這個錯誤。 – WCByrne 2015-09-03 19:46:22

+0

在我的情況下,它是通過嘗試顯示一個UIWebView觸發的,該UIWebView是從一個NSData對象創建的,這個對象的數據可能是無效的,可能來自一個空文件。 – jk7 2016-09-23 23:56:56

相關問題