Set line height in UITextView
IphoneCocoa TouchIphone Problem Overview
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...
Iphone Solutions
Solution 1 - Iphone
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;
Solution 2 - Iphone
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.
Solution 3 - Iphone
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.
Solution 4 - Iphone
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.
Solution 5 - Iphone
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
Solution 6 - Iphone
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.
Solution 7 - Iphone
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.
Solution 8 - Iphone
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.
Solution 9 - Iphone
Use OHAttributedLabel Lib. This will solve all the problem you mentioned.