本文介绍了-fontWithSize返回不同​​的字体对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个解决方法:

<$ p $

更新1
$ b $ p> // //而不是
font = [font fontWithSize:pointSize];

//我可以做到这一点,并获得正确的字体
font = pre>




我使用类将HTML变成一个尊重HTML中样式的NSAttributedString。但是,我在我的应用程序中使用自定义字体,出于某种原因,我的字体总是以粗体显示。我没有使用系统字体的这个问题。我已经缩小到这个方法:

$ p $ - (UIFont *)字体
{
/ / **代码路径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 {
// **代码路径2 **
font = [font fontWithSize:pointSize];
}
返回字体;



$ b

这个方法被调用了很多次。在第一次迭代期间,如果我在 Code Path 1 和log font 设置一个断点,我会得到我期待:

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

但是,如果我注销 font 代码路径2 之后,我得到了这个:
$ b $ $ pre $ < UICTFont:0x7fd41bd709a0> font-family:Fort; font-weight:bold; font-style:normal; font-size:18.00pt

通过调用 -fontWithSize: code>,我的字体从 Fort-Book 更改为正常 Fort



fontWithSize 的文档明确指出:

为什么我要回来一个不同的字体对象?

解决方案

原来这是iOS 8.3(也许更早)的一个bug。
$ b

我提出了一个雷达(#21540510)。

您可以在这里看到:



同时,如果您遇到 -fontWithSize 的问题,请改用 + fontWithName:size:。 b $ b

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:

Why would I be getting back a different font object?

解决方案

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.

这篇关于-fontWithSize返回不同​​的字体对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:33