TTStyledText problems with ampersand (‘&’)

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;"];
  1. Thanks for posting your code. Were you able to open the “Maps” application with this? I was only able to open safari with this URL. I am trying to open the Maps application. Would appreciate any pointers? THanks.

  2. Thank you !
    That saved me a lot of time !

  3. Flavio Luiz Maria

    Very useful this tip! Thanks!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>