简体   繁体   English

UILabel外观字体和属性字符串字体

[英]UILabel appearance font and attributed string font

In my app I have a global custom font applied to all labels like so: 在我的应用程序中,我将全局自定义字体应用于所有标签,如下所示:

UIFont *font = [UIFont fontWithName:kMyFontName size:15.0]; 
[[UILabel appearance] setFont:font];

This works fine. 这很好用。 However, in some cases I want to be able to specify a different font for a specific region of a UILabel string. 但是,在某些情况下,我希望能够为UILabel字符串的特定区域指定不同的字体。

So I have something like this: 所以我有这样的事情:

NSString *string = @"Foo Bar Baz";
UIFont *boldFont = [UIFont fontWithName:kMyBoldFontName size:15.0]; 
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
[attrString setAttributes:@{ NSFontAttributeName: boldFont } range:NSMakeRange(0, 3)];
self.myLabel.attributedText = attrString;

However this doesn't seem to work. 然而,这似乎不起作用。 I expect the "Foo" to be bold, but the entire string just has the default font. 我希望“Foo”是粗体,但整个字符串只有默认字体。 It's as if the bold font is not applied at all and is being overwritten by the font set on the UILabel appearance proxy. 就好像粗体字体根本没有应用,并且被UILabel外观代理上的字体集覆盖。

When I remove the UILabel appearance line then it works fine (I can see part of the string in bold). 当我删除UILabel外观线然后它工作正常(我可以看到粗体字符串的一部分)。 Basically I want to have my custom font applied to the label but a separate font applied to a different region of the string. 基本上我想将自定义字体应用于标签,但是应用于字符串的不同区域的单独字体。 Normally this works fine with attributed strings but for some reason setting the UILabel appearance font disables this functionality (or so it seems). 通常这适用于属性字符串但由于某种原因设置UILabel外观字体禁用此功能(或似乎如此)。

  • Expected results: " Foo Bar Baz" 预期成果:“ Foo Bar Baz”
  • Actual results: "Foo Bar Baz" 实际结果:“Foo Bar Baz”

If I remove the [[UILabel appearance] setFont:] line then it works: 如果我删除[[UILabel appearance] setFont:]行,那么它的工作原理如下:

  • " Foo Bar Baz" Foo Bar Baz”

(but the custom font is not set on the rest of the string). (但是字符串的其余部分没有设置自定义字体)。

So my question is: Is there a way to specify a single font to use as the default app-wide but still be able to partially override that using attributed strings? 所以我的问题是:有没有办法指定一个字体用作默认的应用程序范围,但仍然可以使用属性字符串部分覆盖它?

Also if someone can explain to me why this is not working I'd appreciate it. 此外,如果有人可以向我解释为什么这不起作用我会很感激。

在设置属性字符串之前将font和textColor设置为nil。

You can't mix and match attributed text and plain text; 您不能混合和匹配属性文本和纯文本; that's why removing the setFont method works - because when you use it, it assumes a plaintext UILabel. 这就是为什么删除setFont方法的原因 - 因为当你使用它时,它假设一个明文UILabel。

NSString *string = @"Foo Bar Baz";
UIFont *boldFont = [UIFont fontWithName:kMyBoldFontName size:15.0]; 
// Define your regular font
UIFont *regularFont = [UIFont fontWithName:kMyFontName size:15.0];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
// And before you set the bold range, set your attributed string (the whole range!) to the new attributed font name
[attrString setAttributes:@{ NSFontAttributeName: regularFont } range:NSMakeRange(0, string.length - 1)];
[attrString setAttributes:@{ NSFontAttributeName: boldFont } range:NSMakeRange(0, 3)];
self.myLabel.attributedText = attrString;

There are two important parts: 有两个重要部分:

  1. UIAppearance applies at the moment of adding UI element to window UIAppearance适用于将UI元素添加到窗口的时刻
  2. labelInstance.font = ... resets all font attributes of currently set attributed string labelInstance.font = ...重置当前设置的属性字符串的所有字体属性

So if you want to keep UIAppearance customisation you have to set your custom attributed string after your label get added to window. 因此,如果要保留UIAppearance自定义,则必须在将标签添加到窗口后设置自定义属性字符串。

Reference article about how does UIAppearance work: Peter Steinberger's cool article 关于UIAppearance如何UIAppearance参考文章: Peter Steinberger的酷文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM