简体   繁体   English

更新NSTextStorage字体时崩溃

[英]Crash when updating NSTextStorage font

My UITextView responds to UIContentSizeCategoryDidChangeNotification to respond to dynamic font changes, and I implemented the following code: 我的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];

It always crashes on the last line, [textStorage endEditing]; 它总是在最后一行崩溃, [textStorage endEditing]; . I have enabled exception breakpoints, but there is no additional information in the debug pane see the backtrace below. 我已启用异常断点, 但调试窗格中没有其他信息, 请参阅下面的回溯。 If I remove 'beginEditing and endEditing , it crashes on the removeAttribute line. 如果我删除'beginEditingendEditing ,它会在removeAttribute行崩溃。 I'm using [UIFont fontWithDescriptor: [UIFontDescriptor preferredFixedFontDescriptorWithTextStyle: IDFontTextStyleFixed] size: 0.0f]; 我正在使用[UIFont fontWithDescriptor: [UIFontDescriptor preferredFixedFontDescriptorWithTextStyle: IDFontTextStyleFixed] size: 0.0f]; all over my app, and it works everywhere, except for in the UITextView . 我的应用程序遍布各处,除了UITextView外,它可以在任何地方使用。

Anyone sees what's going wrong and how I could fix it? 任何人都会看到出了什么问题以及如何解决它?

EDIT: here's the backtrace: 编辑:这是回溯:

    (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 

OK, I solved it. 好的,我解决了。

I am using a helper class to handle the NSTextStorage , NSTextContainer , and NSLayoutManager objects. 我正在使用一个帮助器类来处理NSTextStorageNSTextContainerNSLayoutManager对象。 When creating MyTextView , I didn't hold on to the helper object (I only passed the textContainer to init of MyTextView ). 在创建MyTextView ,我没有抓住辅助对象(我只将textContainer传递给MyTextView init )。 I now made the helper object a @property of MyTextView so it has a strong reference, and the crash is gone. 我现在辅助对象一个@propertyMyTextView所以它具有很强的参考和崩溃消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我使用NSUnderlineStyleAttributeName在NSTextStorage上设置属性时,UITextView文本丢失了字体大小 - UITextView text is loosing font size when i set an Attribute on NSTextStorage with NSUnderlineStyleAttributeName NSTextStorage的子类导致访问链接崩溃 - Subclass of NSTextStorage causes crash on access link 删除字符后NSTextStorage子类崩溃 - NSTextStorage subclass crash upon removing characters 在NSTextStorage NSMutableAttributedString的末尾设置新字体 - Set new font at end of NSTextStorage NSMutableAttributedString 附加文本时,未更新由NSTextStorage支持的UITextView - UITextView backed by NSTextStorage not updated when text appended 使用字体时iOS 10崩溃 - iOS 10 Crash when using font 更新字体时更新网页视图页码 - Update webview page number when updating font 为 NSTextStorage 子类设置 viewController 的 layoutManager 时字形索引无效 - Invalid glyph index when setting viewController's layoutManager for NSTextStorage subclass 在某些情况下,NSTextStorage子类无法处理表情符号字符和更改字体 - NSTextStorage subclass can't handle emoji characters and changes font in some cases 为什么在设置斜体字体时CTFramesetterCreateWithAttributedString会崩溃? - Why does CTFramesetterCreateWithAttributedString crash when setting italic font?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM