How to increase line spacing in UITextView

How to increase line spacing in UITextView?

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

+5
source share
2 answers

Declare that you implement the protocol by adding

<NSLayoutManagerDelegate>

to your interface. Then install:

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 API NSMutableParagraphStyle setLineSpacing:.

In order to provide the always useful copy-paste function of the snippet code, here you go!

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

^ myViewElementmaybe UITextField, UILabelor UITextView.

+8
source

In IOS6 +, you can set input attributes for UITextView

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

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

[textView setTypingAttributes:attrsDictionary];
+4
source

All Articles