Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make part of a string bold in iOS?

Tags:

string

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?

like image 742
WISHY Avatar asked Dec 01 '14 06:12

WISHY


People also ask

How do I make text bold in IOS?

Open the Settings app . In the Settings app, select Accessibility from the list. On the Accessibility screen, select Display & Text Size. On the Display & Text Size screen, select Bold Text to set the toggle switch to On.

How do you make string bold in Objective C?

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName]; NSString *yourString = ...; NSRange boldedRange = NSMakeRange(22, 4); NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString]; [attrString beginEditing]; [attrString addAttribute:kCTFontAttributeName ...


1 Answers

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];
like image 105
Abhishek Avatar answered Oct 05 '22 23:10

Abhishek