59

I'm already pretty sure that it can't be done with any public API, but I still want to ask:

Is there any way to change the line height in a UITextView?

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

thanks, Max

EDIT: I know that there is UIWebView and that it's nice and can do styling etc. But it's not editable. I need a editable text component with acceptable line height. That thing from the Omni Frameworks doesn't help either, as it's too slow and doesn't feel right...

0

9 Answers 9

114

After iOS 7, the styleString approach no longer works.

Two new alternatives are available.

Firstly, TextKit; a powerful new layout engine. To change line spacing, set the UITextView's layout manager's 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's lineSpacing. This gives even more control, e.g. first line indentation, and calculation of a bounding rect. 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, e.g NSFontAttributeName

self.textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello World over many lines!" attributes:attrsDictionary];

FWIW, the old contentInset method to align the text along the left edge of UITextView is also no use under iOS7. Instead, to remove the margin:

textView.textContainer.lineFragmentPadding = 0;
4
  • 5
    The only problem with this is that you can't use it to make the line height smaller Sep 24, 2013 at 14:40
  • how do you calculate the height of the text in textview then? Oct 2, 2013 at 4:27
  • @TimWachter Have you tried to use negative values? Works for me.
    – kubilay
    Nov 10, 2014 at 13:54
  • @MichałZygar the second approach creates an NSAttributedString, which has methods to give you the required bounding rect. Dec 30, 2014 at 5:00
25

Note: this is not available in iOS7:


I have discovered that you can create a subclass that re-implements [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 private API usage: it is just subclassing. It's possible Apple may disagree of course (though considering how we all used to swizzle everything in order to customize UIKit appearance, I feel that this kind of “private” usage is not what Apple object to), but it's such an easy way to achieve the goals of this question that you may as well try it. Should the app be rejected you can spend the (probably significant) time on a more difficult solution.

12
  • 1
    Interesting grey-zone approach. ;) Jan 17, 2012 at 12:01
  • 1
    It works on UITextView not UIView. Tested with iOS 5 in an app I use everyday.
    – mxcl
    Feb 29, 2012 at 18:13
  • 5
    It's safe, I have used it in one of my apps since version 1.0. Now I am at version 1.0.4 and all apps have passed the appstore review!
    – Alexander
    Jan 9, 2013 at 12:13
  • 4
    styleString is not available in iOS7
    – Fattie
    Nov 25, 2013 at 12:30
  • 1
    No longer works because UITextView is no longer backed by WebKit, but rather TextKit as of iOS 7.
    – aehlke
    May 3, 2015 at 18:43
21

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

UITextView's Attribute Inspector

1
  • Works! :) Thanks for sharing!
    – DaleK
    Aug 21, 2017 at 2:17
19

The only solution we've found and the one we've chosen: create a custom font. Sound silly but seems to be the only realistic way.

6
  • @ Max Seelemann: Have you ever found another solution to this problem or is this the only way? I guess FontLab will do and then simply resize font to base line and you'll have a better spacing, right? Thanks!
    – n.evermind
    Apr 1, 2011 at 14:06
  • @n.evermind No we still use this solution. And as far as I can tell, iOS 5 will not change anything to that... Jul 14, 2011 at 12:24
  • That's quite annoying. Do you thin coreText is an alternative? I just can't be bothered learning something new yet again... for something so simple (line spacing).
    – n.evermind
    Jul 14, 2011 at 12:40
  • Well you can consider Core Text. But that has many more disadvantages, as I described here: stackoverflow.com/questions/3642540/… Jul 14, 2011 at 15:05
  • 1
    You have to use a font editing software like FontLab Studio. As I didn't do the customization, I can't tell which exact tool we used and how to do it. HTH anyways. Aug 23, 2012 at 10:33
10

The UITextView subclass override of styleString only works if you define a category on UITextView that defines styleString, otherwise you get a compile error. For example, in your UITextView subclass:

#import "SomeDangTextView.h"

@interface UITextView ()

- (id)styleString;

@end

@implementation SomeDangTextView

- (id)styleString {
    return [[super styleString] stringByAppendingString:@"; line-height: 1.5em"];
}

@end
3
  • I tried Max Howell's answer before without success, dinked around in Apple's docs for awhile, and gave up. But this (defining a category on UITextView) is what got it to work. Thanks very much. Mar 19, 2012 at 7:52
  • This worries me as a non-public (aka private) API. I dont see it documented: developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/…
    – Mazyod
    May 31, 2012 at 10:48
  • This still isn't working for me. How do you define -(id)styleString in the UITextView category? Apr 18, 2013 at 17:11
5

This question is almost 10 years old but this is how it's done:

Just implement the following method of UITextViewDelegate and set your attributes:

let textViewAttributes: [NSAttributedString.Key:Any] = [
    .font: UIFont.systemFont(ofSize: 15, weight: .medium),
    .foregroundColor: UIColor.black,
    .paragraphStyle: {
        let paragraph = NSMutableParagraphStyle()
        paragraph.lineSpacing = 4
        return paragraph
    }()
]

func textViewDidBeginEditing(_ textView: UITextView) {
    textView.typingAttributes = textViewAttributes
}

It's important to add these attributes on textViewDidBeginEditing as the dictionary gets reset every time the text selection changes. More info can be found on the official documentation.

1
  • It doesn't seem to be necessary to do this as late as when the textViewDidBeginEditing delegate method is called. I'm setting typingAttributes right after instantiating the UITextView and getting the desired effect. Nov 7, 2021 at 17:47
-1

According to Apple's Documentation you can use a UIWebView.

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

3
  • It is if you intercept the tap on the WebView, show a UITextView on top, edit and save into the UIWebView ;)
    – Jordan
    Sep 21, 2010 at 17:11
  • right, but as the primary usage is while editing this still is not the best option. And there's another problem: It's very very hard (if at all possible) to manually place the insertion point in a UITextView upon an event. This, however, would be needed to switch views. :( Sep 22, 2010 at 0:26
  • UITextField adopts UITextInput which includes - (UITextPosition *)closestPositionToPoint:(CGPoint)point, etc... sounds like what you need?
    – nielsbot
    Dec 20, 2011 at 21:51
-1

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

1
  • -1 That's not what the question was about. Plus it's also stated there that UIWebView is no choice since it does not offer editing. Mar 26, 2013 at 10:24
-4

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

1
  • 1
    Sorry, but it doesn't. OHAttributedLabel is a UILabel -- a noneditable component. The question is about UITextView which is editable. Jan 17, 2012 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.