Setting line height in UITextView

I am already sure that this cannot be done with any public API, but I still want to ask:

Is it possible to change the row height in a UITextView?

It would be enough to do this statically, no need to change it at runtime. The problem is that the default line height is just the WAY too small. The text will look extremely compressed and is a nightmare when trying to write longer texts.

thanks max

EDIT: I know there is a UIWebView , and it is nice and can do style, etc. But this is not editable. I need an editable text component with an acceptable line height. This thing from Omni Frameworks doesn't help either, as it is too slow and doesn't feel good ...

+54
iphone cocoa-touch
Sep 21 '10 at 13:45
source share
8 answers

After iOS 7, the styleString style no longer works.

Two new alternatives are available.

Firstly, TextKit; powerful new layout engine. To change line spacing, set the UITextView layout manager delegate:

 textView.layoutManager.delegate = self; // you'll need to declare you implement the NSLayoutManagerDelegate protocol 

Then override this delegate method:

 - (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect { return 20; // For really wide spacing; pick your own value } 



Secondly, iOS 7 now supports NSParagraphStyle lineSpacing. This gives even more control, for example. indent the first line and calculate the bounding box. So alternatively ...

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.headIndent = 15; // <--- indention if you need it paragraphStyle.firstLineHeadIndent = 15; paragraphStyle.lineSpacing = 7; // <--- magic line spacing here! NSDictionary *attrsDictionary = @{ NSParagraphStyleAttributeName: paragraphStyle }; // <-- there are many more attrs, eg NSFontAttributeName self.textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello World over many lines!" attributes:attrsDictionary]; 



FWIW, the old contentInset method for aligning text along the left edge of a UITextView is also not used in iOS7. Instead, to remove the field:

 textView.textContainer.lineFragmentPadding = 0; 
+97
Sep 18 '13 at 15:33
source share

Note: this is not available in iOS7:




I found that you can create a subclass that [UITextView styleString] :

 @interface UITextView () - (id)styleString; // make compiler happy @end @interface MBTextView : UITextView @end @implementation MBTextView - (id)styleString { return [[super styleString] stringByAppendingString:@"; line-height: 1.2em"]; } @end 

This is not a personal use of the API: it is just a subclass. Perhaps Apple, of course, may disagree (although, given the way we are all accustomed to adjusting the look and feel of UIKit, I believe that using this kind of โ€œprivateโ€ is not an object of Apple), but itโ€™s such an easy way to achieve The goals of this question you can try. If the application is rejected, you can spend (probably significant) time on a more complex solution.

+24
Dec 20 2018-11-21T00:
source share

The only solution we found and the one we chose: create our own font. It sounds silly, but it seems like this is the only realistic way.

+19
Oct. 12 '10 at 11:40
source share

In the Attribute Inspector for UITextView, instead change the Text property to Attributed (from Plain ) and click the "more" button, there you can set the height and spacing of the line.

UITextView Attributes Inspector

+12
May 6 '16 at 15:20
source share

The priority of a UITextView subclass of the String style only works if you define a category in the UITextView that defines the styleString, otherwise you will get a compilation error. For example, in a subclass of UITextView:

 #import "SomeDangTextView.h" @interface UITextView () - (id)styleString; @end @implementation SomeDangTextView - (id)styleString { return [[super styleString] stringByAppendingString:@"; line-height: 1.5em"]; } @end 
+10
Mar 02 2018-12-12T00:
source share

According to Apple Documentation, you can use UIWebView.

This class does not support multiple styles for text. The font, color, and text attributes that you specify always apply to all text view content. To display a more complex style in your application, you need to use the UIWebView object and display its contents using HTML.

-one
Sep 21 2018-10-21
source share

This class does not support multiple styles for text. The font, color, and text attributes that you specify always apply to all text view content. To display a more complex style in your application, you need to use the UIWebView object and display its contents using HTML.

-one
Mar 22 '13 at 9:47 on
source share

Use the OHAttributedLabel Lib. This will solve the whole problem you mentioned.

-four
Dec 21 2018-11-12T00:
source share



All Articles