Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-fontWithSize returning different font object

Update 1

I found a work-around:

// instead of
font = [font fontWithSize:pointSize];

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

I'm using Nick Lockwood's HTMLLabel class to turn HTML into an NSAttributedString that respects the styling within the HTML. For the most part, it works great.

However, I'm using a custom font in my app, and for some reason, my font is always getting rendered in bold. I don't have this problem using the system fonts. I've 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 at Code Path 1 and log font, I get what I'm expecting:

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

However, if I log out font after Code Path 2, I get this:

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

Somehow, by calling -fontWithSize:, my font is getting changed from Fort-Book to regular Fort.

The documentation for fontWithSize clearly states:

Returns a font object that is the same as the receiver but which has the specified size instead.

Why would I be getting back a different font object?

like image 817
djibouti33 Avatar asked Jun 25 '15 04:06

djibouti33


1 Answers

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

I filed a radar (#21540510).

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

In the meantime, if you're having problems with -fontWithSize, use +fontWithName:size: instead.

like image 70
djibouti33 Avatar answered Oct 24 '22 04:10

djibouti33