How to increase spacing between lines in UITextView?
I wish to use default system font,i.e., Helvetica with size 15.
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
.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With