微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决修复 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
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。