Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to have NSString with two font sizes

Is it possible to have one NSString that have the first word one font size / colour and the next word another font size and colour?

I was thinking maybe NSAttributedString - but not sure if thats the way to go?

I tried putting two NSString into NSAttributedString - but that didn't work.

Looking for a UILabel that has something like this:

LARGEString(smallString)

Working Example

So far I have come up with this:

        /* Set the Font Sizes */

UIFont *objectNameFont = [UIFont systemFontOfSize:14.f];
UIFont *itemsFont = [UIFont systemFontOfSize:12.f];

/* Create the attribute dictionaries */

NSDictionary *objectNameDict = [NSDictionary dictionaryWithObject:objectNameFont forKey:NSFontAttributeName];
NSDictionary *objectItemDict = [NSDictionary dictionaryWithObject:itemsFont forKey:NSFontAttributeName];

/* Create the  string */

NSString *labelFullName = [NSString stringWithFormat:@"%@ (%@)", object.name, object.items];

/* Seperate the two strings */

NSArray *components = [labelFullName componentsSeparatedByString:@" ("];
NSRange objectNameRange = [labelFullName rangeOfString:[components objectAtIndex:0]];
NSRange objectItemRange = [labelFullName rangeOfString:[components objectAtIndex:1]];

/* Create the Attributed string */
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:labelFullName];


/* Start the editiong */
[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
                   value:[UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1] /*#aaaaaa*/
                   range:objectItemRange];

[attrString addAttribute: NSForegroundColorAttributeName
                   value:[UIColor colorWithRed:0.271 green:0.271 blue:0.271 alpha:1] /*#454545*/
                   range:objectNameRange];

[attrString addAttributes:objectNameDict range:objectNameRange];

[attrString addAttributes:objectItemDict range:objectItemRange];

[attrString endEditing];
cell.title.attributedText = attrString;


return cell;

Which seems to work as I need it to. However, the separator for the two strings "(" is black and I need it to be same colour as object.items colour which is set here:

[attrString addAttribute: NSForegroundColorAttributeName value:[UIColor colorWithRed:0.667 green:0.667 blue:0.667 alpha:1] /*#aaaaaa*/ range:objectItemRange];

SOLUTION FOUND

I found a solution that works for me:

I pulled the strings out and put them into an NSArray and used objectAtIndex to get their NSRange values to set the text Any ideas?

Thanks all.

like image 722
Robert J. Clegg Avatar asked Mar 20 '23 01:03

Robert J. Clegg


1 Answers

Yes you can have.

Also you got the correct Class for that NSAttributedString.

In the following code two font sizes 12 and 15 are used:

UIFont *font=[UIFont fontWithName:@"Arial" size:12.f];
NSDictionary *attrsDict=[NSDictionary dictionaryWithObject:font
                                forKey:NSFontAttributeName];
NSAttributedString *attribString=[[NSAttributedString alloc] initWithString:[words[0] substringFromIndex:1]  attributes:attrsDict];

UIFont *fontFirst=[UIFont fontWithName:@"Arial" size:15.f];
NSDictionary *attrsDictFirst=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
NSAttributedString *firstString=[[NSAttributedString alloc] initWithString:[attribString subStringToIndex:1]  attributes:attrsDictFirst];

[attribString replaceCharactersInRange:NSMakeRange(0, 1)  withString:firstString];

For more, similar questions are here and here.

like image 94
Anoop Vaidya Avatar answered Mar 27 '23 16:03

Anoop Vaidya