ios - 同一标签中的不同字体大小?

标签 ios uilabel nsattributedstring uifont

是否可以在同一个 UILabel 中使用不同的字体大小或粗细?我可以在 Storyboard 中将其作为属性标签来完成,但我需要以编程方式进行。

cell.lblOne.text = [NSString stringWithFormat:
                       @"FontSize15:: %@, FontSize20:: %@",monkey, goat];

编辑:我看到了一些关于 NSAttributedString 的东西,但我无法让它工作。

最佳答案

看看我的回答:

UITextView Alternative

  • 制作一个 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/

相关文章:

ios - 在 Swift 中,如何更改字符串的一部分的属性?

ios - UITableView 控件上的不同行为

ios - 由于 "GCKConnectionSuspendReasonNetworkNotReachable",Cast session 暂停

ios - 如何在 iOS 13 的 NFCTagReaderSession 中读取不同类型的 NFC 标签?

ios - NSMutableAttributedString包含NSMutableAttributedString和NSString

ios - NSTextAttachment 以某种方式缓存图像。关于如何做有什么想法吗?

iOS : Readjusting a UITableViewCell according the height of a UIImage in Swift

ios - 多个 UILabel 之间的固定边距

ios - uilabel的水平滚动?

ios - 快速动画不工作