UILabel外观字体和属性字符串字体 [英] UILabel appearance font and attributed string font

查看:96
本文介绍了UILabel外观字体和属性字符串字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将全局自定义字体应用于所有标签,如下所示:

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];

这很好用。但是,在某些情况下,我希望能够为UILabel字符串的特定区域指定不同的字体。

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.

所以我有这样的事情:

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;

然而这似乎不起作用。我希望Foo是粗体,但整个字符串只有默认字体。就好像粗体字体根本没有被应用,并被UILabel外观代理上的字体集覆盖。

However this doesn't seem to work. I expect the "Foo" to be bold, but the entire string just has the default font. 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外观线时,它的工作原理很好(我可以用粗体看到部分字符串)。基本上我想将自定义字体应用于标签,但是应用于字符串的不同区域的单独字体。通常这适用于属性字符串但由于某种原因设置UILabel外观字体会禁用此功能(或者似乎如此)。

When I remove the UILabel appearance line then it works fine (I can see part of the string in bold). 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).


  • 预期结果: Foo Bar Baz

  • 实际结果:Foo Bar Baz

  • Expected results: "Foo Bar Baz"
  • Actual results: "Foo Bar Baz"

如果我删除 [[UILabel外观] setFont:] 行,那么它可以工作:

If I remove the [[UILabel appearance] setFont:] line then it works:


  • 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.

推荐答案

您不能混合和匹配属性文本和纯文本;这就是为什么删除 setFont 方法的原因 - 因为当你使用它时,它会假定一个明文UILabel。

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.

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;

这篇关于UILabel外观字体和属性字符串字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆