IPhone: change line spacing in UITextView?

I searched all over the network for information / examples ...

I am trying to change the line spacing of a text inside a UITextView object to double the distance. I thought you could do it through Core Text, but could not find a solution!

Any sample code or information is welcome! Thanks!

+6
text iphone line spacing paragraph
source share
3 answers

You do not need to watch on the net. Viewing the documentation for a UITextView sufficient to determine that line spacing is not supported by this control.

With Core Text, of course, you have full control over the layout of the text you draw. But it would be a lot of work to rewrite the UITextView control from scratch.

+5
source share

This question was asked before iOS 6. I would like to post an updated answer for those who would like to do this.

This can now be done using iOS 6 and later using NSAttributedString. UITextView now accepts the attributed string as one of its properties. You can perform all kinds of manipulations with attributes with bound strings, including line spacing. You can set the minimum and maximum line heights for the line paragraph style attribute.

See the NSAttributedString Class link for more information.

Here is an example of what you could do:

 NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; paragraph.minimumLineHeight = 21.0f; paragraph.maximumLineHeight = 21.0f; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Test text line 1\nTest text line 2\nTest text line 3" attributes:@{NSParagraphStyleAttributeName: paragraph}]; textView.attributedText = attributedString; 
+4
source share

You can use NSLayoutManagerDelegate. Add this delegate to the ViewController or UIView class (etc.), and then when you create your UITextView ...

 yourTextView.layoutManager.delegate = self 

Add this delegate method:

 func layoutManager(layoutManager: NSLayoutManager, lineSpacingAfterGlyphAtIndex glyphIndex: Int, withProposedLineFragmentRect rect: CGRect) -> CGFloat { return 5 //Whatever you'd like... } 
+1
source share

All Articles