NSAttributedString的示例有两种不同的字体大小? [英] Example of NSAttributedString with two different font sizes?

查看:66
本文介绍了NSAttributedString的示例有两种不同的字体大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSAttributedString 对我来说真是难以理解。

NSAttributedString is just really impenetrable to me.

我想设置 UILabel 以获得不同大小的文本,我收集 NSAttributedString 是可行的方法,但我无法获得有关此文档的任何内容。

I want to set a UILabel to have text of different sizes, and I gather NSAttributedString is the way to go, but I can't get anywhere with the documentation on this.

如果有人可以提供帮助,我会很高兴我有一个具体的例子。

I would love it if someone could help me with a concrete example.

例如,假设我想要的文字是:

For instance, let's say the text I wanted was:

(in small letters:) "Presenting The Great..."
(in huge letters:) "HULK HOGAN!"

有人可以告诉我该怎么做吗?或者甚至,我可以为自己学习的简单明了的参考文献?我发誓我已经尝试通过文档来理解这一点,甚至通过Stack Overflow上的其他示例,我只是没有得到它。

Can somebody show me how to do that? Or even, a reference that's plain and simple where I could learn for myself? I swear I've tried to understand this through the documentation, and even through other examples on Stack Overflow, and I'm just not getting it.

推荐答案

你会做这样的事情......

You would do something like this…

NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:20.0]
              range:NSMakeRange(24, 11)];

这将设置20点文本中的最后两个单词;字符串的其余部分将使用默认值(我认为是12分)。设置文本大小可能令人困惑的是你必须同时设置字体大小 - 每个 UIFont 对象封装了这两个属性。

This will set the last two words in 20-point text; the rest of the string will use the default value (which I believe is 12 points). The thing that might be confusing about setting the text size is that you have to set the typeface and the size at the same time—each UIFont object encapsulates both of those properties.

这篇关于NSAttributedString的示例有两种不同的字体大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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