修复 UITextView 中修改段落间距的光标大小

问题描述

我有一个自定义的、可编辑的 UITextView,我修改了段落间距,如下所示:

func layoutManager(_ layoutManager: NSLayoutManager,paragraphSpacingBeforeGlyphAt glyphIndex: Int,withProposedLineFragmentRect rect: CGRect) -> CGFloat {
    return 10
}

func layoutManager(_ layoutManager: NSLayoutManager,paragraphSpacingAfterGlyphAt glyphIndex: Int,withProposedLineFragmentRect rect: CGRect) -> CGFloat {
    return 10
}

这会导致光标非常大。我试图通过覆盖 caretRect:

解决这个问题
override func caretRect(for position: UITextPosition) -> CGRect {
    let defaulCaretRect = super.caretRect(for: position)
    return CGRect(x: defaulCaretRect.origin.x,y: defaulCaretRect.origin.y,width: defaulCaretRect.width,height: 22)
}

它在某些情况下运行良好,但在其他情况下,origin.y错误的:

wrong origin.y

如果我调整 origin.y,它会在正确的情况下破坏它。我不知道如何识别 origin.y 是否需要修复。我错过了什么吗?

在这里看到了一些使用以下内容的旧答案:

rect.size.height = font.pointSize - font.descender

但由于某种原因,这没有效果。有什么想法吗?

最后,较大的段落间距也会导致选择句柄过大:

enter image description here

还有什么办法可以解决吗?

解决方法

我想我已经想通了。正如在 SO 上发现的其他问题中所建议的那样,用字体调整插入符号的高度似乎效果很好。缺少的是修复插入符号的来源 Y。

我认为 Apple 给我们 UITextPosition 肯定是有原因的。使用它,我能够获得行文本和段落文本。有了这些,我就可以检查插入符号是否在导致问题的段落的第一行。

override func caretRect(for position: UITextPosition) -> CGRect {
    var superRect = super.caretRect(for: position)
    
    guard let paragraphRange = tokenizer.rangeEnclosingPosition(position,with: .paragraph,inDirection: .storage(.backward)),let paragraphText = text(in: paragraphRange)else { return superRect }
    
    guard let lineRange = tokenizer.rangeEnclosingPosition(position,with: .line,let lineText = text(in: lineRange) else { return superRect }
    
    guard let font = font else { return superRect }
    
     if paragraphText.hasPrefix(lineText) {
        superRect.origin.y += 10
    }
    
    superRect.size.height = font.pointSize - font.descender
    
    return superRect
}

不过,我不知道如何修复大的选择手柄。