Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel with custom font wrongly rendered

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.

like image 661
bompf Avatar asked Mar 16 '12 16:03

bompf


1 Answers

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...

like image 122
bompf Avatar answered Nov 04 '22 23:11

bompf