UILabel with custom font display incorrect

In my iPhone application, I set a custom font for all UILabels (more precisely, I subclassed UILabel by overriding the method, setting my own font in this method, and then setting all the labels in IB for this custom class). The problem is that all texts are too far below the expected baseline, so letters such as "y" and "g" are truncated below. I read about similar issues here:

UIButton Custom Font Alignment

Custom installed font does not display correctly in UILabel

Then I tried messing around with the clamping device as described in these solutions (it was originally set to 990). Installing it around 500 led to good results, but soon after that I noticed that the lines in multi-line texts mix with each other, which, of course, is unacceptable. In UITextViews, the font seems to look great with the source base, though ..

Is there a practical way to solve this problem? Of course, I could store several fonts with different ascending lines for texts with several or single-line texts, but this is a rather dirty solution.

PS: The font is provided in otf format, although I tried to convert it to ttf, which led to the same results.

+6
source share
1 answer

Well, just in case someone is interested, I came up with a workaround that should work for me. It just consists in overriding the drawTextInRect of UILabel , modifying the given rectangle and passing it 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 may have to try different values ​​other than 0.25, though ...

+6
source

Source: https://habr.com/ru/post/910931/


All Articles