NSAttributedString does my nut in. It seems like a lot of rigmarole for "dammit, I just want these three words in red" - so I did a little homework and found you could go via rtf.
So, the example above - wanting a few words in red. An rtf document exists between a pair of curly braces...
{\rtf1 This is a minimal rtf document}
For colours you need to create a colour table, also inside some curly braces, then select the colours from that...
{\rtf1 {\colortbl;\red255\green0\blue0;\red0\green0\blue255;}This is \cf1 red \cf0 and this is \cf2 blue\cf0.}
To render this into an attributed string you basically pretend that you scored a pile of rtf data from somewhere. So, if our rtf string is called (suprise) rtfString the magic line to return an attributed string is...
[[NSAttributedString alloc] initWithRTF:[rtfString dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:nil]
Noting two things: That 'documentAttributes' can be used with the
traditional attributes dictionary to set a document wide attribute such as font; and that if you're creating an rtf string in code you need to remember to double backslash i.e. '\' is typed as '\\' otherwise it gets interpreted as a control character.
The
rtf format reference is here and you can apparently pull the same trick with HTML although whether this is actually any easier depends on one's attitude towards HTML I suppose.