如何在iOS中使字符串的一部分变为粗体? [英] How to make part of a string bold in iOS?

查看:82
本文介绍了如何在iOS中使字符串的一部分变为粗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本字符串的某些部分加粗.

I want to make some part of a text string bold.

例如:这是粗体.这是正常的字符串.

Eg: This is to be bold. This is normal string.

在Android中,可以使用跨度字符串轻松实现.在iOS中相当于什么?

In Android, it can be easily achieved by using spannable strings. What is its equivalent in iOS?

推荐答案

是的,可以使用

Yes, it can be achieved with NSAttributedString:

NSString *yourString = @"This is to be bold. This is normal string.";
NSMutableAttributedString *yourAttributedString = [[NSMutableAttributedString alloc] initWithString:yourString];
NSString *boldString = @"This is to be bold";
NSRange boldRange = [yourString rangeOfString:boldString];
[yourAttributedString addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:boldRange];
[yourLabel setAttributedText: yourAttributedString];

这篇关于如何在iOS中使字符串的一部分变为粗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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