Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView lineSpacing cause different cursor height between paragraph lines

I'm using NSMutableParagraphStyle in my UITextview for adding linespace between each row text.

When I type something in textview, cursor height is normal. but when I move cursor position to text on the 2nd row (not the last row), cursor height is getting bigger.

big caret

What should I do to make cursor height normal in every row of the texts? This is code I'm currently using:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 30.; textView.font = [UIFont fontWithName:@"Helvetica" size:16]; textView.attributedText = [[NSAttributedString alloc] initWithString:@"My Text" attributes:@{NSParagraphStyleAttributeName : paragraphStyle}]; 
like image 601
novalagung Avatar asked Nov 26 '13 03:11

novalagung


People also ask

How to set the line height and spacing in UITextView?

In the Attribute Inspector for the UITextView instead change the property Text to Attributed (from Plain), and click the "more" button, there you can set the line height and spacing.

How to change line spacing in iOS 7 textkit?

Firstly, TextKit; a powerful new layout engine. To change line spacing, set the UITextView's layout manager's delegate: Then override this delegate method: Secondly, iOS 7 now supports NSParagraphStyle's lineSpacing. This gives even more control, e.g. first line indentation, and calculation of a bounding rect. So alternatively...

What are the alternatives to stylestring in UITextView?

After iOS 7, the styleString approach no longer works. Two new alternatives are available. Firstly, TextKit; a powerful new layout engine. To change line spacing, set the UITextView's layout manager's delegate:

Is it possible to change the line height at runtime?

Would be enough to do it statically, no need to change it at runtime. The problem is that the default line height is just WAY too small. Text will look extremely compressed and is a nightmare when trying to write longer texts.


1 Answers

Finally I found a solution that solve my problem.

Changing the cursor height is possible by subclassing the UITextView, then overriding the caretRectForPosition:position function. For example:

- (CGRect)caretRectForPosition:(UITextPosition *)position {     CGRect originalRect = [super caretRectForPosition:position];     originalRect.size.height = 18.0;     return originalRect; } 

Documentation link: https://developer.apple.com/documentation/uikit/uitextinput/1614518-caretrectforposition


Update: Swift 2.x or Swift 3.x

See Nate's answer.


Update: Swift 4.x or Swift 5.x

For Swift 4.x use caretRect(for position: UITextPosition) -> CGRect.

import UIKit  class MyTextView: UITextView {      override func caretRect(for position: UITextPosition) -> CGRect {         var superRect = super.caretRect(for: position)         guard let font = self.font else { return superRect }          // "descender" is expressed as a negative value,          // so to add its height you must subtract its value         superRect.size.height = font.pointSize - font.descender          return superRect     } } 

Documentation link: https://developer.apple.com/documentation/uikit/uitextinput/1614518-caretrect

like image 185
novalagung Avatar answered Oct 05 '22 23:10

novalagung