iphone - 更改 NSString 中的 html 标签

标签 iphone html objective-c ios ipad

例如我有 html 字符串:

<p>
    <img mce_src="http://example.com/apple.png" src="http://example.com/apple.png" width="512" height="512" style="">
    <br mce_bogus="1">
</p>

如何更改此属性:width="512"height="512"例如:width="123"height="123"

谢谢

最佳答案

你可以使用

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 
                                        withString:(NSString *)replacement

您的示例,其中的 html 位于 htmlString

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

编辑:

使用正则表达式替换(未测试):

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(.*width=\").*?(\".*?height=\").*?(\".*)"
                                                                       options:NSRegularExpressionCaseInsensitive
                                                                         error:&error];

NSString *modifiedString = [regex stringByReplacingMatchesInString:htmlString
                                                           options:0
                                                             range:NSMakeRange(0, [htmlString length])
                                                      withTemplate:@"$1<insert width here>$2<insert height here>$3"];

引用: NSRegularExpression

关于iphone - 更改 NSString 中的 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11952788/

相关文章:

javascript - 尝试动态调整图像上图标的大小

objective-c - 通过所有可用网络接口(interface)使用 GCDAsyncUdpSocket 进行 UDP 广播

ios - 设置 FetchedResultsController 中的特殊结果

iphone - 使用发送者标签在UIbutton单击时更新UITableViewCell的标签

iphone - Objective C 中的 UTF8 字符解码

iphone - 在 Objective-C 中绑定(bind)模型类的方法

ios - 无法单击 UIScrollView 内的组件

javascript - 是否所有 html 节点都有 "getElementsBy"和 getElementBy"版本?

javascript - 如何确定坐标是否在 SVG 关闭路径内?

ios - NSNotification 与 UITextFieldDelegate