我正在尝试使用NSTextContainer的excludePaths来排除UITextView中的一个正方形,例如:
NSTextStorage* textStorage = [[NSTextStorage alloc] initWithAttributedString:attributedString];
NSLayoutManager *layoutManager = [NSLayoutManager new];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.bounds.size];
UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 250, 250)];
textContainer.exclusionPaths = @[rectanglePath];
[layoutManager addTextContainer:textContainer];
self.textView = [[UITextView alloc] initWithFrame:self.bounds textContainer:textContainer];
self.textView.editable = NO;
self.textView.scrollEnabled = NO;
[self addSubview:self.textView];
在iOS 7.0中,这很正常:
然而,在iOS 7.1中,这会导致NSTextContainer的lineFragmentRectForProposedRect:atIndex:writingDirection:remainingRect:
函数无限循环,占用了99%的CPU并且像疯子一样泄漏内存。应用程序变得完全不响应,并最终由于内存使用而终止。显然,这是iOS 7.1中的一个错误。
当我将排除矩形的x原点仅更改一个点(从原来的{0,0}到{1,0})时,它可以工作,但看起来很糟糕:
只有当第一行的第一个字符受到排除矩形的影响时,才会发生此错误。当我将排除矩形更改为{0,30}时,它也可以工作:
但显然这不是我想要的。有人知道如何解决这个问题吗?