i_am_jorf i_am_jorf - 11 days ago 9
Objective-C Question

How can I make a UITextView layout text the same as a UILabel?

I have a

UILabel
that I need to convert to a
UITextView
because reasons. When I do this, the text is not positioned the same, despite using the same (custom) font.

I found that if I set:

textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;


This gets the text very close, but if I superimpose the
UITextView
over top of the
UILabel
, I see the text positioning get farther apart with each new line.

Screen shot showing text drift.

The
UILabel
is green, the
UITextView
is black. This is using
NSParagraphStyle
to set min and max line height to 15.

I've played with setting the paragraph style and min/max line height, but I haven't been able to match it exactly. I'm not a printer, so I don't necessarily understand all of the font related terms in the documentation for
NSLayoutManager
and
NSTextContainer
and all that.

I only need to support iOS 7 and up.

I'm not going to switch to some crazy CoreText-based custom widget or use some random third party library. I'm okay with close enough if I have to. But it seems like there should be some combination of random properties to make them layout the same.

Answer

I took the solution for line spacing found at this link and applied it to your issue. I managed to get it incredibly close by adjusting the lineSpacing property. I tested with HelveticaNeue size 13 and managed to get it to line up as shown in the screen shot below.

textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.lineSpacing = -0.38;

NSDictionary *attrsDictionary =
@{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13.0f],
 NSParagraphStyleAttributeName: paragraphStyle};

textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attrsDictionary];

Screen Shot

Comments