我想用htmlstring设置UIWebView,并且图像有问题:
...

<img align="absBottom" alt="" height="350" src="http://www.xyz.com//files/userfiles/images/abc.jpg" vspace="10" width="625" />


...

我想将高度和宽度替换为200和320。
我在用着

htmlString = [htmlString stringByReplacingOccurrencesOfString:@"width=\"" withString:@"width=\"320];


然后我得到结果:

<img align="absBottom" alt="" height="350" src="http://www.xyz.com//files/userfiles/images/abc.jpg" vspace="10" width="320625" />


如何从htmlString删除宽度后的数字,或者将宽度替换为320px的正确方法是什么?

最佳答案

使用NSScanner尝试以下操作:

NSScanner *theScanner;
NSString *subStrng =nil;
theScanner = [NSScanner scannerWithString:htmlString];
[theScanner scanUpToString:@"width" intoString:NULL] ;
[theScanner scanUpToString:@" " intoString:&subStrng] ;
htmlString = [htmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@", subStrng] withString::@"width=\"320\""];
NSLog("%@",subStrng);

10-08 12:23