错误呈现自定义字体的UILabel [英] UILabel with custom font wrongly rendered

查看:107
本文介绍了错误呈现自定义字体的UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,我为所有UILabel设置了自定义字体(更准确地说,我将UILabel子类化,覆盖方法,在该方法中设置自定义字体,然后将IB中的所有标签设置为该自定义类)。现在的问题是,所有文本都被渲染得远远低于预期基线,所以像'y'和'g'这样的字母在下面被截断。我在这里读过类似的问题:

In my iPhone app I set a custom font for all UILabels (to be more precise, I subclassed UILabel, overriding a method, setting the custom font in that method and then set all labels in IB to that custom class). The problem now is, that all texts are rendered too far below the expected baseline, so letters like 'y' and 'g' are cut off below. I've read about similar problems here:

UIButton自定义字体垂直对齐

自定义安装的字体在UILabel中无法正确显示

然后,我试图摆弄ascender,如这些解决方案中所述(最初设定为990)。将它设置为大约500导致了良好的结果,但不久之后,我注意到多行文本中的行被相互混合,这当然是不可接受的。在UITextViews上,字体似乎可以使用初始基线进行渲染。

I then tried fiddling around with the ascender as described in those solutions (it was set to 990 initially). Setting it to around 500 led to good results, but not long after that, I noticed that lines in multiline texts were blended into each other, which of course isn't acceptable. On UITextViews, the font seems to render fine with the initial baseline though..

是否有实用的方法来解决此问题?当然,我可以为多行或单行文本保留几种不同上升字体,但这是一个相当混乱的解决方案..

Is there a practical way to solve this problem? Of course I could keep several fonts with different ascenders for either multi- or single-line texts, but that is a rather messy solution..

PS:提供字体在otf格式,虽然我尝试将其转换为ttf,导致相同的结果。

PS: The font is provided in otf format, though I tried converting it to ttf, leading to the same results.

推荐答案

好的,以防万一有人是感兴趣的是,我想出了一个适用于我的解决方法。它只是重写 drawTextInRect UILabel 的方法,修改给定的矩形并将其传递给超类方法。

Okay, just in case somebody is interested, I figured out a workaround that should work for me. It simply consists of overriding the method drawTextInRect of UILabel, modifying the given rectangle and pass it on to the superclass method.

- (void)drawTextInRect:(CGRect)rect {
    CGFloat pointSize = self.font.pointSize;

    CGRect newRect = CGRectMake(rect.origin.x, rect.origin.y - (0.25 * pointSize), rect.size.width, rect.size.height);

    [super drawTextInRect:newRect];
}

我可能需要尝试除0.25以外的其他值...

I might have to try out different values other than 0.25 though...

这篇关于错误呈现自定义字体的UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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