I'm parsing an RSS feed from a blog, and putting the HTML of a post inside a UIWebView
.
But now I would like to change the size of the images to fit the iPhone screen. I'm trying with the following replace
HTMLContent = [HTMLContent stringByReplacingOccurrencesOfString:@"width=\"480\"" withString:@"width=\"300\""];
But doing like this I'm replacing only the images with 480 width. And moreover I'm not changing the height!
Do you know if there is a way to replace any width with 300, and changing the height by the same factor?
You can do this using a Regular Expression. You'll want to match the
<img>
tags within the string and extract the height and width values, then calculate your new desired height and width, and then replace the height and width in the string with the new values.The following code should work, but it may miss some edge cases (such as if the height attribute appears before the width attribute in the
img
tag).