UITextView以不同于UILabel的方式偏移文本 [英] UITextView offsets text differently than UILabel

查看:244
本文介绍了UITextView以不同于UILabel的方式偏移文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UILabel UITextView ,他们以不同方式呈现文字。似乎 UITextView 将文本偏移4。

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

下面是一个示例,其中顶部是 UILabel 并且低于是 UITextView 。他们都使用相同的字体。这里有两个例子,一个是自定义的 OpenSans 字体,另一个是系统的 HelveticaNeue 字体。

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.

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 设置为( 0,0,0,0)。

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

任何帮助?以下是截图:

Any help? Here are the screenshots:

1.1 HelveticaNeue:textView偏移量-4(顶部标签)

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

1.2 HelveticaNeue:对齐(顶部标签)

1.2 HelveticaNeue: aligned (label on top)

2.1 OpenSans:textView偏移量-4(顶部标签) < br>

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

2.2 OpenSans:对齐(顶部标签)

2.2 OpenSans: aligned (label on top)

推荐答案

这对我有用并消除了内部填充:

This works for me and eliminates the inner padding:

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

这篇关于UITextView以不同于UILabel的方式偏移文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆