Multiple colours in an NSString
or NSMutableStrings
are not possible. So I\'ve heard a little about the NSAttributedString which was introduced wit
Since iOS 7 you can use NSAttributedString
with HTML syntax:
NSURL *htmlString = [[NSBundle mainBundle] URLForResource: @"string" withExtension:@"html"];
NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithFileURL:htmlString
options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
textView.attributedText = stringWithHTMLAttributes;// you can use a label also
You have to add the file "string.html" to you project, and the content of the html can be like this:
firstsecondthird
Now, you can use NSAttributedString
as you want, even without HTML file, like for example:
//At the top of your .m file
#define RED_OCCURENCE -red_occurence-
#define GREEN_OCCURENCE -green_occurence-
#define BLUE_OCCURENCE -blue_occurence-
#define HTML_TEMPLATE @"-red_occurence--green_occurence--blue_occurence-