Reputation:
is it possible to put 2 NSAttributedString
s in only one label?
For example:
NSString *a = [car valueForKey:@"name"];
NSString *b = [car valueForKey:@"version"];
NSAttributedString *title;
title = [[NSAttributedString alloc] initWithString:a attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : @1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //1
NSAttributedString *title2;
title2 = [[NSAttributedString alloc] initWithString:b attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : @0 , NSStrokeColorAttributeName : [UIColor blackColor]}]; //2
cell.textLabel.attributedText = //what should I write here?
Upvotes: 1
Views: 1341
Reputation: 108
You can use the -appendAttributedString:(NSString *) method to append one attributed string to the other. Since you can make both of your attributed strings mutable, you can also include separators (semicolons, commas) to differentiate between your two different strings in the one label.
Here is some sample code:
NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:@"Hello"];
NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:@"Hello 2"];
NSMutableAttributedString *semiStr = [[NSMutableAttributedString alloc] initWithString:@" ; "];
[string1 appendAttributedString:semiStr]; //separate string 1 and 2 by semi-colon
[string1 appendAttributedString:string2];
textLabel.text = string1; //which string2 is now appended to
Obviously, in your attributed strings, you would have attributes (like you stated in your question). You can also always trust Apple's documentation on the NSMutableAttributedString class to find suitable methods to use next time.
Upvotes: 1
Reputation: 119041
You can use NSMutableAttributedString
to either append the two attributed strings together or to build the attributed string and set the attributes at specified ranges (based on the source strings).
Upvotes: 0