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上的其他例子来理解了这一点,但是我还是没有理解。
4 个解决方案
#1
153
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 and the size at the same time—each UIFont
object encapsulates both of those properties.
这将在20点的文本中设置最后两个单词;其余的字符串将使用默认值(我认为是12点)。设置文本大小可能会让人困惑的是,你必须同时设置字体和大小——每个UIFont对象封装了这两个属性。
#2
16
Swift 3 Solution
斯威夫特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) ]))
#3
5
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)]));
#4
0
If you want to do it the easy way, there is a git repo called OHAttributedLabel that I use that provides a category on NSAttributedString. It lets you do things like:
如果你想用简单的方法,有一个叫做OHAttributedLabel的git repo,它在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 link for a decent tutorial on how to get started with attributed strings.
如果您不想使用第三方库,请查看这个链接以获得关于如何使用带属性字符串的教程。
#1
153
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 and the size at the same time—each UIFont
object encapsulates both of those properties.
这将在20点的文本中设置最后两个单词;其余的字符串将使用默认值(我认为是12点)。设置文本大小可能会让人困惑的是,你必须同时设置字体和大小——每个UIFont对象封装了这两个属性。
#2
16
Swift 3 Solution
斯威夫特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) ]))
#3
5
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)]));
#4
0
If you want to do it the easy way, there is a git repo called OHAttributedLabel that I use that provides a category on NSAttributedString. It lets you do things like:
如果你想用简单的方法,有一个叫做OHAttributedLabel的git repo,它在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 link for a decent tutorial on how to get started with attributed strings.
如果您不想使用第三方库,请查看这个链接以获得关于如何使用带属性字符串的教程。