同一标签中的字体大小是否不同? [英] Different font size in the same label?

查看:204
本文介绍了同一标签中的字体大小是否不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在同一UILabel中是否可以具有不同的字体大小或粗细?我可以在故事板中将其作为归因"标签来做,但是我需要以编程方式来做.

Is it possible to have different fontsize or weight in the same UILabel? I can do it in the storyboard as Attributed label, but I need to do it programmatically.

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

我看到了一些关于NSAttributedString的信息,但是我无法使其正常工作.

I saw something about NSAttributedString but I can't get it to work.

推荐答案

在这里看看我的答案:

UITextView替代

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

这篇关于同一标签中的字体大小是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆