0

I parse an XML and use different elements from the xml to build some HTML code that I can then show in a UIWebView. One part of the Parsed XML includes pictures with links. I would like to keep the pictures, but lose the links. Normally I could just replace all the ahref tags in the string, but I would like to keep other links not on pictures. Is there anyway I can do this in XCode to only remove the ahref tags from images?

a href="http://treymorgan.net/wp-content/uploads/2012/06/divorce.jpg" img class="alignleft size-full wp-image-4539" title="divorce" src="http://treymorgan.net/wp-content/uploads/2012/06/divorce.jpg" alt="" width="190" height="150"/ /a

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
user717452
  • 33
  • 14
  • 73
  • 149

1 Answers1

2

You should be able to do this using CSS rather than altering the link itself. Take a look at this article for a suggestion on how to achieve this:-

http://css-tricks.com/pointer-events-current-nav/

Update: Here's a full working example that disables links placed around images on iOS and many desktop browsers. In your example, you should replace the <body> content with your parsed HTML snippet.

<html>
<head>
    <style type='text/css'>
    a > img {
        pointer-events: none;
        cursor: default;
        }
    </style>
</head>
<body>
    <a href='http://www.google.com'>Link to Google</a><br>
    <a href='http://www.google.com'><img src='[image_url.png]'></a>
</body>
</html>
Purpletoucan
  • 6,472
  • 2
  • 21
  • 28
  • I can't really do that...If you look at my question again, I am building the HTML in code, I can't access the link to tell it to remove any pointer events. – user717452 Jul 09 '12 at 18:30
  • 1
    You should be able to. If you are building the HTML to display in the UIWebView, then you should also be able to add some inline CSS styles in the document head. UIWebView adheres to CSS just as a browser does. – Purpletoucan Jul 09 '12 at 18:42
  • Ok, but how would I get it to just remove pointer events on pictures, leaving other links on page intact? – user717452 Jul 09 '12 at 18:46
  • I've just updated my answer to include a code snippet that I think should work using CSS 2.1 selectors, but I haven't tested it. Good luck! – Purpletoucan Jul 09 '12 at 18:52
  • I tried that out, and all it did was add a[href$=jpg], a[href$=jpeg], a[href$=png], a[href$=gif] {pointer-events: none; cursor: default;} To the top of the webview. – user717452 Jul 09 '12 at 19:11
  • You need to make sure that the style sits inside the header. I have updated my code snippet to show a full document structure, your parsed HTML should replace the body content. I have revised the CSS approach and have tested that it works in the iPhone simulator. – Purpletoucan Jul 10 '12 at 07:28