ios - NSTextStorage 的子类导致访问链接崩溃

标签 ios uikit textkit

我正在构建一个自定义 NSTextStorage 类来查找和匹配自定义用户输入并在我的应用程序中处理它。所以我遵循了几个教程,最著名的是 objc.io's ,并创建了一个子类并将其 layoutManager 设置为与通过 nib 创建的 UITextView 相同:

-(void)setCustomTextStorage:(NSTextStorage *)customTextStorage
{
    if (_customTextStorage != customTextStorage)
    {
        _customTextStorage = customTextStorage;
        [_customTextStorage addLayoutManager:self.textView.layoutManager];
    }
}

然后为 NSTextStorage 子类覆盖 processEditing 并在适当的地方添加 NSLinkAttributeName

顺便说一句,示例应用程序可用 here ,请务必检查 custom-text-storage 分支或只阅读此问题的差异 here

现在,当用户点击实际链接时,我得到一个使应用程序崩溃的异常:

2014-08-20 23:06:45.223 JSQMessages[63170:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001025d2495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001021a999e objc_exception_throw + 43
    2   CoreFoundation                      0x00000001025d22ad +[NSException raise:format:] + 205
    3   UIFoundation                        0x000000010558273f -[NSConcreteTextStorage attribute:atIndex:effectiveRange:] + 115
    4   UIKit                               0x00000001013d401b -[UITextView(LinkInteraction) _interactableItemAtPoint:] + 789
    5   UIKit                               0x00000001013d59a4 -[UITextView(LinkInteraction) willInteractWithLinkAtPoint:] + 17
    6   UIKit                               0x000000010108801b -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) gestureRecognizerShouldBegin:] + 305
    7   UIKit                               0x000000010107fc7b -[UIGestureRecognizer _shouldBegin] + 1026
    8   UIKit                               0x000000010107cdfd -[UIGestureRecognizer setState:] + 183
    9   UIKit                               0x0000000100ebcd3f -[UIDelayedAction timerFired:] + 68
    10  Foundation                          0x0000000101d93e14 __NSFireTimer + 83
    11  CoreFoundation                      0x0000000102594c34 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    12  CoreFoundation                      0x00000001025947b2 __CFRunLoopDoTimer + 962
    13  CoreFoundation                      0x000000010257d7be __CFRunLoopRun + 1614
    14  CoreFoundation                      0x000000010257cd83 CFRunLoopRunSpecific + 467
    15  GraphicsServices                    0x0000000103c2ef04 GSEventRunModal + 161
    16  UIKit                               0x0000000100d56e33 UIApplicationMain + 1010
    17  JSQMessages                         0x0000000100014ea3 main + 115
    18  libdyld.dylib                       0x000000010339f5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这很奇怪,因为我在那里设置了一个断点,backingStore 返回一个完全有效的带有属性的 NSDictionary

我很乐意为任何愿意帮助我解决这个令人抓狂的问题的人买啤酒。

注意:也可在 iOS 8 上重现,所以我认为这不是 iOS 错误

最佳答案

遇到了同样的问题。我目前找到的唯一解决方案是以编程方式创建完整堆栈而不是使用 nib:

self.textStorage = [[CustomTextStorage alloc] init];

NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[self.textStorage addLayoutManager:layoutManager];

NSTextContainer *textContainer = [[NSTextContainer alloc] init];
[layoutManager addTextContainer:textContainer];

UITextView *textView = [[UITextView alloc] initWithFrame:frame textContainer:textContainer];
textView.delegate = self;

关于ios - NSTextStorage 的子类导致访问链接崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25414668/

相关文章:

ios - 带有插图的 UIButton 标签文本自动收缩

ios - 如何使用 MonoTouch AudioConverter 将 LinearPCM 转换为压缩格式?

swift - 当我离开当前的 UIViewController 时如何隐藏 tabBar?

ios - 在 viewDidLoad 中分配 UIViewControllers。这是一个错误吗?

ios - 如何使用 iOS7 Text Kit 将文本环绕在附件周围?

iOS 5/6 与 iOS 7 多行标签行间距

ios - 根据调整大小的 UIImage 变换线点

ios - 将资源嵌入二进制作为字节数组的缺点?

ios - 创建基于自动布局的指标 View

ios - UIScrollView内多个UITextView在分页模式交互问题