It is possible to have an NSString with two font sizes

Is it possible to have one NSStringthat has the first word one font size / color and the next word a different font size and color?

I thought maybe NSAttributedString- but not sure if this is the way to go?

I tried putting two NSStringin NSAttributedString, but that didn't work.

We are looking for a UILabel that has something like this:

LARGEString (smallString)

Working example

So far I have come up with the following:

        /* 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;

It seems that I work as I need. However, the two-line separator "(" is black, and I need it to be the same color as the color object.itemsspecified 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:

NSArray objectAtIndex, NSRange ?

.

+4
1

, .

NSAttributedString.

12 15:

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];

, .

+7

Source: https://habr.com/ru/post/1533896/


All Articles