如何在 UILabel 中将某些文本格式化为粗体?
How to format some text as bold within a UILabel?
我有一个 iOS UILabel,它需要一些文本是正常的,一些文本是粗体的。粗体文本实际上应该是应用程序另一部分的 link,但现在,我只是对点击整个标签做出反应。如何将某些文本的格式设置为粗体?
您需要使用 NSAttributedString。请查看此答案以查看示例:Any way to bold part of a NSString?
使用 attributedText
属性:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"This is a test."];
[text addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:12]
range:NSMakeRange(0, 4)];
label.attributedText = text;
我有一个 iOS UILabel,它需要一些文本是正常的,一些文本是粗体的。粗体文本实际上应该是应用程序另一部分的 link,但现在,我只是对点击整个标签做出反应。如何将某些文本的格式设置为粗体?
您需要使用 NSAttributedString。请查看此答案以查看示例:Any way to bold part of a NSString?
使用 attributedText
属性:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"This is a test."];
[text addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:12]
range:NSMakeRange(0, 4)];
label.attributedText = text;