Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView offsets text differently than UILabel

I am using UILabel and UITextView and they render text differently. It seems that UITextView offsets text by 4.

Below is an example where at the top is UILabel and bellow is UITextView. They both use same font. Two examples are here, one with the custom OpenSans font and one with the system's HelveticaNeue font.

UILabel is being resized after setting the text by using sizeThatFits:

    label.text = text;
    CGFloat width = 320 - 2 * 16; // both label and textView end up with 288 width
    CGSize size = [label sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)];
    CGRect frame = CGRectMake(16, 0, width, size.height);
    label.frame = frame;

UITextView.textContainerInset is set to (0,0,0,0).

Any help? Here are the screenshots:

1.1 HelveticaNeue: textView offset -4 (label on top)
HelveticaNeue: offset -4

1.2 HelveticaNeue: aligned (label on top)
HelveticaNeue - aligned

2.1 OpenSans: textView offset -4 (label on top)
OpenSans - offset -4

2.2 OpenSans: aligned (label on top)
OpenSans - aligned

like image 533
vale4674 Avatar asked Jun 28 '15 20:06

vale4674


1 Answers

This works for me and eliminates the inner padding:

textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsZero;
like image 94
Artal Avatar answered Oct 06 '22 09:10

Artal