ios 具有两种不同字体大小的 NSAttributedString 示例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18365631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Example of NSAttributedString with two different font sizes?
提问by Le Mot Juiced
NSAttributedString
is just really impenetrable to me.
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.
我想设置一个UILabel
具有不同大小的文本,我收集NSAttributedString
是要走的路,但我无法获得有关此的文档的任何地方。
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!"
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.
有人可以告诉我怎么做吗?或者甚至是一个简单明了的参考资料,我可以在其中学习?我发誓我已经尝试通过文档,甚至通过 Stack Overflow 上的其他示例来理解这一点,但我只是没有理解。
回答by bdesham
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)];
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 andthe size at the same time—each UIFont
object encapsulates both of those properties.
这将设置 20 点文本中的最后两个单词;字符串的其余部分将使用默认值(我认为是 12 点)。设置文本大小可能令人困惑的一点是,您必须同时设置字体和大小——每个UIFont
对象都封装了这两个属性。
回答by iljn
Swift 3 Solution
Swift 3 解决方案
Also, you can use the append
function instead of specifying indices in ObjC or Swift:
此外,您可以使用该append
函数而不是在 ObjC 或 Swift 中指定索引:
let attrString = NSMutableAttributedString(string: "Presenting The Great...",
attributes: [ NSFontAttributeName: UIFont.systemFont(ofSize: 20) ])
attrString.append(NSMutableAttributedString(string: "HULK HOGAN!",
attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 40) ]))
回答by Kunal Shah
Swift 4 Solution:
斯威夫特 4 解决方案:
let attrString = NSMutableAttributedString(string: "Presenting The Great...",
attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 18)]);
attrString.append(NSMutableAttributedString(string: "HULK HOGAN!",
attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 36)]));
回答by JonahGabriel
If you want to do it the easy way, there is a git repo called OHAttributedLabelthat I use that provides a category on NSAttributedString. It lets you do things like:
如果您想以简单的方式进行操作,我使用了一个名为OHAttributedLabel的 git 存储库,它在 NSAttributedString 上提供了一个类别。它可以让您执行以下操作:
NSMutableAttributedString *mystring = [[NSMutableAttributedString alloc] initWithString:@"My String"];
[mystring setTextColor:[UIColor colorWithRGB:78 green:111 blue:32 alpha:1]];
mystring.font = [UIFont systemFontOfSize:14];
If you don't want to use a 3rd party lib, check out this linkfor a decent tutorial on how to get started with attributed strings.
如果您不想使用第 3 方库,请查看此链接以获取有关如何开始使用属性字符串的体面教程。