TTStyledText problems with ampersand (’&')

by Warlock on Apr.25, 2010, under Cocoa, iPhone

I ran across an interesting problem today where I was trying to use TTStyledText from the Three20 project to create a TTStyledTextLabel that had text with links nested within it. I was trying to

Map here.

I was doing this with the following (simplified code):

[TTStyledText textFromXHTML:@"Map <a href=\"http://www.google.com/maps?f=q&hl=en\">here</a>." lineBreaks:YES URLs:YES];

As soon as I tried to put a URL in that had multiple URL parameters (and thus an &) things would disappear.

From briefly looking at the Three20 source code, I believe this because the parsing of the XHTML text is being done with an XML parser. As soon as I replaced the &s with &amp;s things started to work. This can be done using the following code:

[@"Map <a href=\"http://www.google.com/maps?f=q&hl=en\">here</a>." stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...