iOS 在UILabel显示不同的字体和颜色

转自:http://my.oschina.net/CarlHuang/blog/138363

在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的attributedText, 使用不同颜色和不同字体的字符串,我们可以使用NSAttributedText 和 NSMutableAttributedText 类来实现。

现实代码:

1
2
3
4
@interface  ViewController : UIViewController
@property  ( nonatomic , strong) IBOutlet  UILabel *attrLabel;
- ( IBAction )next:( id )sender;
@end

 .m文件 在viewDidLoad方法中添加以下代码:

1
2
3
4
5
6
7
8
9
self .title = @ "For iOS 6 & later" ;
NSMutableAttributedString  *str = [[ NSMutableAttributedString  alloc] initWithString:@ "Using NSAttributed String" ];
[str addAttribute: NSForegroundColorAttributeName  value:[UIColor blueColor] range: NSMakeRange (0,5)];
[str addAttribute: NSForegroundColorAttributeName  value:[UIColor redColor] range: NSMakeRange (6,12)];
[str addAttribute: NSForegroundColorAttributeName  value:[UIColor greenColor] range: NSMakeRange (19,6)];
[str addAttribute: NSFontAttributeName  value:[UIFont fontWithName:@ "Arial-BoldItalicMT"  size:30.0] range: NSMakeRange (0, 5)];
[str addAttribute: NSFontAttributeName  value:[UIFont fontWithName:@ "HelveticaNeue-Bold"  size:30.0] range: NSMakeRange (6, 12)];
[str addAttribute: NSFontAttributeName  value:[UIFont fontWithName:@ "Courier-BoldOblique"  size:30.0] range: NSMakeRange (19, 6)];
attrLabel.attributedText = str;

 

效果图:

如果想在iOS6.0以前版本实现这个效果,需要使用到一个第三方库TTTAttributedLabel,同时还有导入CoreText.frame框架.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 中的 UILabel 是一种用于显示文本的控件,它可以用于显示降价信息。你可以使用具有 Markdown 格式的 UILabel,例如使用 YYText 组件。YYText 是 YYKit 的一个组件,它是一个功能强大的 iOS 文本框架,用于显示和编辑富文本。它具有高性能的异步文本布局和渲染,并且扩展了 CoreText 的属性,提供了更多的文本效果。你可以使用 NSMutableAttributedString 来创建富文本字符串,并将其设置给 UILabel 的 attributedText 属性,以实现降价信息的显示。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [CDMarkdownKit:广泛的Swift框架,提供简单且可自定义的markdown解析](https://download.csdn.net/download/weixin_42099087/15035498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [YYText:用于显示和编辑富文本的 iOS 文本框架-开源](https://download.csdn.net/download/weixin_42116672/20385582)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [GitHub - ElvisCen/PPCounter: 一款简单实用的数字加减动画,支持UILabel、UIButton显示](https://blog.csdn.net/weixin_36159799/article/details/117478907)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值