How to fix vertical selection handles in a modified paragraph style in UITextView (iOS)?

This is the problem:

enter image description here

What I have done?

I have set a paragraph Style to this text:

 let titleParagraphStyle = NSMutableParagraphStyle()
 titleParagraphStyle.alignment = .natural
 titleParagraphStyle.paragraphSpacing = 33

 let attributes = [NSParagraphStyleAttributeName: titleParagraphStyle]

This of course made the cursor to be HUGE all the time, so after this I overrided caretfor and all is OK until selection of the text:

override func caretRect(for position: UITextPosition) -> CGRect {
        var original = super.caretRect(for: position)
        guard let isFont = self.font else { return original }
        original.size.height = isFont.pointSize - isFont.descender
        return original
    }

What I am missing here? How can I make the selection handles to be equal size than the text?

1 answer

  • answered 2017-06-17 17:57 Aaron Zheng

    Currently you are literally changing the spacing attribute of your text which, in no way is possible to change the selecting cursor's size. What you can do is just programmatically make spacing in the textDidChange when you detect that the yourTextField.attributedText?.size().width passes a certain distance. I presume that you aren't copying and pasting, so just that will work. Let me know otherwise.