是否可以在同一个 UILabel 中使用不同的字体大小或粗细?我可以在 Storyboard 中将其作为属性标签来完成,但我需要以编程方式进行。
cell.lblOne.text = [NSString stringWithFormat:
@"FontSize15:: %@, FontSize20:: %@",monkey, goat];
编辑:我看到了一些关于 NSAttributedString 的东西,但我无法让它工作。
最佳答案
看看我的回答:
- 制作一个 NSMutableAttributedString
- 给它一些属性(应用于字符范围)
- 设置标签的 attributedText 属性
.
NSMutableAttributedString *attString =
[[NSMutableAttributedString alloc]
initWithString: @"monkey goat"];
[attString addAttribute: NSForegroundColorAttributeName
value: [UIColor redColor]
range: NSMakeRange(0,6)];
[attString addAttribute: NSFontAttributeName
value: [UIFont fontWithName:@"Helvetica" size:15]
range: NSMakeRange(0,6)];
[attString addAttribute: NSFontAttributeName
value: [UIFont fontWithName:@"Didot" size:24]
range: NSMakeRange(7,4)];
self.label.attributedText = attString;
关于ios - 同一标签中的不同字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14377866/