5

How to increase spacing between lines in UITextView?

I wish to use default system font,i.e., Helvetica with size 15.

user7388
  • 1,741
  • 2
  • 19
  • 25
nitz19arg
  • 417
  • 7
  • 18
  • possible duplicate of [How to control the line spacing in UILabel](http://stackoverflow.com/questions/5494498/how-to-control-the-line-spacing-in-uilabel) – Sulthan Apr 03 '13 at 12:24
  • http://stackoverflow.com/questions/4277551/iphone-uitextview-set-line-spacing – Anoop Vaidya Apr 03 '13 at 12:24
  • http://stackoverflow.com/questions/3760924/set-line-height-in-uitextview – Anoop Vaidya Apr 03 '13 at 12:24
  • so in short, I cannot do it as the other topics suggest. – nitz19arg Apr 04 '13 at 05:45
  • You can definitely do this. Please accept an answer or submit your solution. – jungledev Oct 01 '15 at 19:24
  • @nitz19arg please accept my answer below (you can do so by selecting the check mark under the upvote/downvote icons) if it worked for you, or submit your solution. It's not good etiquette to leave answers hanging.. – jungledev Oct 15 '15 at 15:40

2 Answers2

9

Declare you implement the protocol by adding

<NSLayoutManagerDelegate>

to your interface. Then, set:

yourTextView.layoutManager.delegate = self;

Then override this delegate method:

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
    return 5; // Line spacing of 19 is roughly equivalent to 5 here.
}

UPDATE: I recently discovered that this can also be done using the NSMutableParagraphStyle setLineSpacing: API.

In an effort to always provide useful copy-paste code-snippet awesomeness, here you go!

NSMutableParagraphStyle *myStyle = [[NSMutableParagraphStyle alloc] init];
[myStyle setLineSpacing:myLineSpacingInt];
[myString addAttribute:myDesiredAttribute value:myStyle range:myDesiredRange];
[myViewElement setAttributedText:myString];

^myViewElement can be a UITextField, UILabel, or UITextView.

jungledev
  • 4,195
  • 1
  • 37
  • 52
5

In IOS6+ you can set the typing attributes for a UITextView

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];

[textView setTypingAttributes:attrsDictionary];
Rami
  • 181
  • 1
  • 5