Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UITextView: Increase line spacing without changing the height of the cursor?

I've been banging my head against a wall on this for too long. I'd like to create double-spaced input text with the cursor staying the same as the font's line height.

I've tried a number of approaches:

  • Using NSLayoutManager's delegate -layoutManager:lineSpacingAfterGlyphAtIndex:withProposedLineFragmentRect:
  • Using an attributed string and setting NSParagraphStyle's -lineHeightMultiple and -lineSpacing properties
  • Using a custom NSTextStorage that also tried setting that paragraph style.

...but they all end up with a carat that's just too big, either extending into the whitespace above or the whitespace below each line.

Any thoughts on what I can try next to achieve this layout?

like image 927
cdownie Avatar asked Mar 22 '23 00:03

cdownie


1 Answers

If you already have desired layout and the problem is just size of caret, you can simply subclass UITextView and override the following method:

- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    CGRect *originalRect = [super caretRectForPosition:position];
    // Resize the rect. For example make it 75% by height:
    originalRect.size.height *= 0.75;
    return originalRect;
}

This'll do the trick, this works on both ios6 and 7 for me

like image 57
Petro Korienev Avatar answered Apr 06 '23 12:04

Petro Korienev