WISHY WISHY -4 years ago 164
iOS Question

How to make part of a string bold in iOS?

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

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

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

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download