UILabel中的字体不正确 - Ascender或Descender被裁剪 [英] Fonts not fitting properly in UILabel - Ascender or Descender cropped

查看:601
本文介绍了UILabel中的字体不正确 - Ascender或Descender被裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了大量的搜索/阅读/测试,并且找不到解决这个问题的办法。
我从iOS 4.3开始试过,但在iOS7中仍然没有解决。



问题是这样的:可以将他们的上升或下降裁剪成UILabel。
这里是直接从Xcode 5.1用户界面(没有任何代码)显示问题的屏幕截图 - 字体大小为300点:



正如你所看到的,即使是像Helvetica这样简单的字体Neue(大胆与否)是Descender裁剪。 (你正在看UIViewController> UIView> UILabel)

如果你尝试这个,然后改变点的大小,你会看到字体缩小,最终下降不被裁剪。这里又是160点:


我在这里谈到高度 - 我知道我可以使用adjustsFontSizeToFitWidth = YES来查看整个长度,我也知道我可以使用sizeToFit ,但是两者都不能保证没有剪切上升/下降。



还要注意,使用Ascender / Descender值来计算高度并没有帮助,因为主要的问题是绘制时字体 不是在标签内垂直居中 。 (如果是的话,这将是一个简单的计算。)

所以这里是一个问题:我怎样才能显示一个字体尽可能高,并确保上升/ descender不会裁剪不管使用的字体



编辑:
我重读了我的问题,我没有正确地问 - 我能够调整标签以适应字体 - 这不是问题。这里是修改后的问题:



如何在UILabel中绘制尽可能大的文本,并确保其垂直居中, Ascender or Descender?



我可以很容易的计算出文本的整体高度,而且一旦我知道它会适合,怎么能在UILabel中垂直居中呢? / p>

例如:在第一个屏幕截图中,文本Tg被裁剪,但是它很容易缩短到足以在标签中垂直放置。事实上,如果它是正确的中心,它可能会更大,并且仍然适合。但我知道没有办法垂直居中...

解决方案

我试过这个,它解决了我的问题。本质上,字母的高度是上升+下降。这就是标签所需的垂直空间。

1 [commentLabel sizeToFit]; //从标签中删除不需要的区域

2。 [commentLabel setFrame:CGRectMake(commentLabel.frame.origin.x,commentLabel .frame.origin.y + ABS(commentLabel.font.descender),commentLabel.frame.size.width,commentLabel.font.ascender + ABS(commentLabel.font.descender))];



// **(2)**中的框架调整通过commentLabel.font.descender向下移动标签,因为默认情况下标签是根据其实际底线而不是我们使用的实际线笔记本电脑,在那里下行垂直下线。在标签的情况下,底线是下方的下端。


I've done extensive searching/reading/testing and cannot find a solution to this problem. I've tried since iOS 4.3 and it's still not resolved in iOS7.

The problem is this: Fonts at large sizes can have their Ascenders or Descenders cropped in a UILabel. Here's a screenshot directly from the Xcode 5.1 UI (no code at all!) showing the problem - font size 300 points:

As you can see, even a simple font like Helvetica Neue (bold or not) has it's Descender cropped. (You're seeing UIViewController > UIView > UILabel)

If you try this and then change the point size you'll see the font scale down, and eventually the Descender will not be cropped. Here it is again at 160 points:

Notice also that some fonts do not get cropped and others do - try Noteworthy, or Papyrus, or Savoye LET - all of which are standard iOS & fonts....

I'm talking about Height here - I know I can use adjustsFontSizeToFitWidth=YES to see the entire length, and I also know I can use sizeToFit, however neither guarantees no cropping of the Ascender/Descender.

Notice also that calculating the height using Ascender/Descender values does not help as the main issue is that the font is not centered vertically within the label when it is drawn. (If it were, it would be a simple calculation.)

So here is the question: How can I show a font as tall as possible and be assured that the Ascender/Descender is not cropped regardless of the font used?

EDIT: I re-read my question and realized I did not ask it properly - I'm able to resize the label to fit the font - that's not the problem. Here's the revised question:

How can I draw text in a UILabel as large as possible and be assured that it is centered vertically, with no cropping of the Ascender or Descender?

I can easily figure out the overall height of the text, and once I know it will fit, how can draw it in the UILabel vertically centered?

For Example: In the first screenshot, the text "Tg" is cropped, but it is easily short enough to fit vertically in the label. In fact, it could be even larger and still fit if it were properly centered. But I know of no way to center it vertically...

解决方案

I tried this and it solved my problem. Essentially, the height of the letter is Ascent+Descent. So that's all the space the label needs vertically.

1. [commentLabel sizeToFit]; //To trim out the unwanted area from the label

2. [commentLabel setFrame:CGRectMake(commentLabel.frame.origin.x, commentLabel.frame.origin.y + ABS(commentLabel.font.descender), commentLabel.frame.size.width, commentLabel.font.ascender + ABS(commentLabel.font.descender))];

//The frame adjustment in **(2)**moves the label down by commentLabel.font.descender because the label by default is aligned based on their actual bottom line instead of the actual line we use on notebooks, where the descender hangs down from the line. In case of a label the bottom line is the lower tip of the descender.

这篇关于UILabel中的字体不正确 - Ascender或Descender被裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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