-fontWithSize returning different font object

99封情书 提交于 2020-01-01 05:26:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/31041318/fontwithsize-returning-different-font-object

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