UILabel with custom font wrongly rendered

久未见 提交于 2019-12-08 19:53:33

问题


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 custom font vertical alignment

Custom installed font not displayed correctly in UILabel

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: The font is provided in otf format, though I tried converting it to ttf, leading to the same results.


回答1:


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];
}

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



来源:https://stackoverflow.com/questions/9741222/uilabel-with-custom-font-wrongly-rendered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!