Ameya Vaidya
Ameya Vaidya

Reputation: 127

Bolding an nsstring

I have looked online for ways to bold a NSString and either it's not there or I can't seem to find it. I simply want to bold the word in the NSString but so far, what I have done is not woking:

 NSString *player1Name = [[UIFont boldSystemFontOfSize:12] fontName];

I have relied on this to make "player1Name" bold but it doesn't work at all. When I run it I get this:

enter image description here

Thanks in advance to anyone who helps me figure out the solution.

Upvotes: 0

Views: 861

Answers (3)

Sam Warfield
Sam Warfield

Reputation: 71

You can not bold a NSString... Try a NSAttributedString...

NSAttributedString *player1Name = [[NSAttributedString alloc] initWithString:@"name" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:12]}];

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/

fontName is a property on UIFont

[[UIFont boldSystemFontOfSize:12] fontName]

returns the font name HelveticaNeueInterface-MediumP4

Upvotes: 2

DilumN
DilumN

Reputation: 2897

You can't make NSString bold. Instead of that you can make UILabel text into bold. Because UILabels are for UI representation & NSStrings are just objects. So make your UILabel bold

[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];

Upvotes: 0

Mehul Sojitra
Mehul Sojitra

Reputation: 1181

Try this:

NSString *myString = @"My string!";
NSAttributedString *myBoldString = [[NSAttributedString alloc] initWithString:myString
                                                             attributes:@{ NSFontAttributeName: [UIFont boldSystemFontOfSize:35.0] }];

Also you can set range for particular text like:

NSString *boldFontName = [[UIFont boldSystemFontOfSize:12] fontName];
NSString *yourString = ...;
NSRange boldedRange = NSMakeRange(2, 4);

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:yourString];

[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName 
                   value:boldFontName
                   range:boldedRange];

[attrString endEditing];

Upvotes: 0

Related Questions