-fontWithSize, returning another font object

Update 1

I found a workaround:

// instead of font = [font fontWithSize:pointSize]; // i can do this and get the correct font back font = [UIFont fontWithName:font.fontName size:pointSize] 

I use the Nick Lockwood HTMLLabel class to turn HTML into an NSAttributedString that matches the style in HTML. For the most part, it works great.

However, I use my own font in my application, and for some reason my font is always rendered in bold. I do not have this problem using system fonts. I narrowed it down to this method:

 - (UIFont *)font { // ** Code Path 1 ** UIFont *font = _styles[HTMLFont]; if ([font isKindOfClass:[NSString class]]) { font = [UIFont fontWithName:(NSString *)font size:17.0f]; } NSInteger pointSize = [_styles[HTMLTextSize] floatValue] ?: font.pointSize; if (self.bold) { if (self.italic) { font = [font boldItalicFontOfSize:pointSize]; } else { font = [font boldFontOfSize:pointSize]; } } else if (self.italic) { font = [font italicFontOfSize:pointSize]; } else { // ** Code Path 2 ** font = [font fontWithSize:pointSize]; } return font; } 

This method is called many times. During the first iteration, if I set a breakpoint in Code Path 1 and log font , I get what I expect:

 <UICTFont: 0x7fd41bc8f2b0> font-family: "Fort-Book"; font-weight: bold; font-style: normal; font-size: 18.00pt 

However, if I exit font after Code Path 2 , I get the following:

 <UICTFont: 0x7fd41bd709a0> font-family: "Fort"; font-weight: bold; font-style: normal; font-size: 18.00pt 

Somehow, by calling -fontWithSize: my font changes from Fort-Book to a regular Fort .

The documentation for fontWithSize clearly states:

Returns a font object that matches the receiver, but which has the specified size.

Why should I return another font object?

+6
source share
1 answer

It turns out this is a bug in iOS 8.3 (maybe earlier).

I filed a radar (# 21540510).

You can see for yourself: https://github.com/djibouti33/UIFontBug

At the same time, if you are having problems with -fontWithSize , use +fontWithName:size:

+4
source

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


All Articles