Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase spacing between lines in UITextView

How to increase spacing between lines in UITextView?

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

like image 555
nitz19arg Avatar asked Apr 03 '13 12:04

nitz19arg


2 Answers

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.

like image 131
jungledev Avatar answered Oct 28 '22 10:10

jungledev


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];
like image 40
Rami Avatar answered Oct 28 '22 09:10

Rami