I'm having a custom NSLayoutManager with these two methods overwritten:
override func drawGlyphs(forGlyphRange glyphsToShow: NSRange, at origin: CGPoint) {
super.drawGlyphs(forGlyphRange: glyphsToShow, at: origin)
let characterRange = self.characterRange(forGlyphRange: glyphsToShow, actualGlyphRange: nil)
textStorage?.enumerateAttribute(.blur, in: characterRange, options: .longestEffectiveRangeNotRequired, using: { (value, subrange, _) in
guard let key = value as? String, !key.isEmpty else { return }
let blurGlyphRange = glyphRange(forCharacterRange: subrange, actualCharacterRange: nil)
drawBlur(forGlyphRange: blurGlyphRange)
textStorage?.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], range: blurGlyphRange)
})
}
private func drawBlur(forGlyphRange tokenGlypeRange: NSRange) {
guard let textContainer = textContainer(forGlyphAt: tokenGlypeRange.location, effectiveRange: nil) else { return }
let withinRange = NSRange(location: NSNotFound, length: 0)
enumerateEnclosingRects(forGlyphRange: tokenGlypeRange, withinSelectedGlyphRange: withinRange, in: textContainer) { (rect, _) in
let blurRect = rect.offsetBy(dx: self.textContainerOriginOffset.width, dy: self.textContainerOriginOffset.height)
UIColor.red.setFill()
UIBezierPath(roundedRect: blurRect, cornerRadius: 4).fill()
}
Everything works fine except when I set the UITextView isScrollingEnabled on false I enter an endless loop caused by the textStorage enumerateAttribute method in drawGlyphs.
I don't understand why this happens and also I don't know how to prevent this. Someone who knows more about this?
EDIT
If I remove the textStorage addAttributes with the foregroundColor then it works. So that's causing the loop for some reason.