UITextView lineHeightMultiple Clips Top, first line, of Text

In iOS 8, I have a vanilla

UITextView
that clips the top of the 1st line when a
lineHeightMultiple
is applied to it's
NSMutableParagraphStyle
, see image below:



It appears as though

lineHeightMultiple
affects the 1st line of text in addition to subsequent lines.


Setting

clipsToBounds = false
on the
UITextView
will at least enable the clipped part to show, but you can see from the image below that now the top part of the text is obviously above it's frame:



I can fix this by just setting the top constraint on the offending

UITextView
to compensate for
clipsToBounds = false
but that feels like a hack to me.


I have also tried using a

WKWebView
for the offending text, and just setting the css
line-height
property, and that works just fine. There must simply be something I am missing when it comes to UITextView though.


Additionally, setting

lineSpacing
on the paragraph style to less than 0 has no affect, per the docs:


The distance in points between the bottom of one line fragment 
and the top of the next. 

**This value is always nonnegative.** 

This value is included in the line fragment heights in the 
layout manager.



I have also tried setting the

contentInset
of the
UITextView
as well as using a system font, both had not affect.

My sample code for this setup follows:


let text = "THIS IS A MULTILINE RUN OF TEXT" 

let font = UIFont(name: "MyFontName", size: 31.0)!
// let font = UIFont.systemFontOfSize(31.0)

let paragraph = NSMutableParagraphStyle() 
paragraph.lineHeightMultiple = 0.75 
paragraph.alignment = NSTextAlignment.Center 

let attributes = [ NSParagraphStyleAttributeName: paragraph, NSFontAttributeName: font ]

titleView.attributedText = NSAttributedString(string: text, attributes: attributes) 
// titleView.contentInset = UIEdgeInsets(top: 50.0, left: 0.0, bottom: 0.0, right: 0.0)
titleView.clipsToBounds = false


Has anyone encountered this and overcome or is the top constraint hack the only way to go?

Replies

The good news is the new Apple forums can't display images.... WHAT!? I don't understand how it's a developer forum, but it's probbaly the worst developer "asking a question experince" I have ever seen... ANYWAY, I digress!


Here are the links to the images from my original post:


1) https://dl.dropboxusercontent.com/u/20065272/forums/ios_lineHeightMultiple/textClipping.png

2) https://dl.dropboxusercontent.com/u/20065272/forums/ios_lineHeightMultiple/clipsToBounds.png


My original question can also be found here:

http://stackoverflow.com/questions/30845705/uitextview-lineheightmultiple-clips-top-first-line-of-text

I had the same issue.
I compensated it using NSBaselineOffsetAttributeName.