簡體   English   中英

更新NSTextStorage字體時崩潰

[英]Crash when updating NSTextStorage font

我的UITextView響應UIContentSizeCategoryDidChangeNotification以響應動態字體更改,我實現了以下代碼:

 NSTextStorage * textStorage = [self textStorage];
[textStorage beginEditing];
[textStorage enumerateAttribute: NSFontAttributeName
                        inRange: NSMakeRange(0, [textStorage length])
                        options: 0
                     usingBlock: ^(id value, NSRange range, BOOL * stop) {
                         UIFont *newFont = [UIFont fontWithDescriptor: [UIFontDescriptor preferredFixedFontDescriptorWithTextStyle: IDFontTextStyleFixed] size: 0.0f];

                         if (newFont) {
                             [textStorage removeAttribute: NSFontAttributeName
                                                    range: range];
                             [textStorage addAttribute: NSFontAttributeName
                                                 value: newFont
                                                 range: range];
                         }
                     }];

[textStorage endEditing];

它總是在最后一行崩潰, [textStorage endEditing]; 我已啟用異常斷點, 但調試窗格中沒有其他信息, 請參閱下面的回溯。 如果我刪除'beginEditingendEditing ,它會在removeAttribute行崩潰。 我正在使用[UIFont fontWithDescriptor: [UIFontDescriptor preferredFixedFontDescriptorWithTextStyle: IDFontTextStyleFixed] size: 0.0f]; 我的應用程序遍布各處,除了UITextView外,它可以在任何地方使用。

任何人都會看到出了什么問題以及如何解決它?

編輯:這是回溯:

    (lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x10f3c0060)
    frame #0: 0x000000010c77f29d libobjc.A.dylib`realizeClass(objc_class*) + 145
    frame #1: 0x000000010c783125 libobjc.A.dylib`lookUpImpOrForward + 127
    frame #2: 0x000000010c792554 libobjc.A.dylib`_objc_msgSend_uncached + 68
    frame #3: 0x0000000118d39ce7 UIFoundation`-[NSGlyphGenerator generateGlyphsForGlyphStorage:desiredNumberOfCharacters:glyphIndex:characterIndex:] + 251
    frame #4: 0x0000000118d06d47 UIFoundation`-[NSLayoutManager(NSPrivate) _fillGlyphHoleForCharacterRange:startGlyphIndex:desiredNumberOfCharacters:] + 736
    frame #5: 0x0000000118d091b0 UIFoundation`_NSFastFillAllGlyphHolesForCharacterRange + 726
    frame #6: 0x0000000118d47f2e UIFoundation`-[NSLayoutManager glyphRangeForCharacterRange:actualCharacterRange:] + 64
    frame #7: 0x0000000118d071af UIFoundation`-[NSLayoutManager(NSPrivate) _fillLayoutHoleForCharacterRange:desiredNumberOfLines:isSoft:] + 617
    frame #8: 0x0000000118d08e9a UIFoundation`-[NSLayoutManager(NSPrivate) _fillLayoutHoleAtIndex:desiredNumberOfLines:] + 208
    frame #9: 0x0000000118d0954a UIFoundation`-[NSLayoutManager(NSPrivate) _markSelfAsDirtyForBackgroundLayout:] + 325
    frame #10: 0x0000000118d13d10 UIFoundation`-[NSLayoutManager(NSPrivate) _invalidateLayoutForExtendedCharacterRange:isSoft:invalidateUsage:] + 2212
    frame #11: 0x0000000118d44743 UIFoundation`-[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 226
    frame #12: 0x0000000118d448f9 UIFoundation`-[NSLayoutManager processEditingForTextStorage:edited:range:changeInLength:invalidatedRange:] + 47
    frame #13: 0x0000000118d6c216 UIFoundation`-[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 168
    frame #14: 0x0000000118d6bd4c UIFoundation`-[NSTextStorage processEditing] + 372
    frame #15: 0x0000000118d6b995 UIFoundation`-[NSTextStorage endEditing] + 83
  * frame #16: 0x000000010bee435b MyApp`-[MyUITextView updateFonts](self=0x00007fc26208c000, _cmd="updateFonts") at MyUITextView.m:107 

好的,我解決了。

我正在使用一個幫助器類來處理NSTextStorageNSTextContainerNSLayoutManager對象。 在創建MyTextView ,我沒有抓住輔助對象(我只將textContainer傳遞給MyTextView init )。 我現在輔助對象一個@propertyMyTextView所以它具有很強的參考和崩潰消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM