gpt4 book ai didi

html - 如何在 iOS 上的 html 文件中创建和替换行

转载 作者:行者123 更新时间:2023-11-29 03:24:03 26 4
gpt4 key购买 nike

我的项目中有一个 html 模板文件,我有一个包含 0 到 10.000 项的数组。我喜欢创建尽可能多的 <img src="link" style="width:30.5mm;height:30.5mm;" class="addBorder"></img> 副本并充满 link与数组索引处的项目。

例如:数组有5个元素

for 循环后的 html 文件应如下所示:

<html>
<style>
.addBorder {
border: 29.5px solid #ffffff;
}
</style>
<body>
<table>
<tr>

<img src="file:///Users/David/Desktop/1_1STKK.png" style="width:30.5mm;height:30.5mm;" class="addBorder"></img>
<img src="file:///Users/David/Desktop/2_1STKK.png" style="width:30.5mm;height:30.5mm;" class="addBorder"></img>
<img src="file:///Users/David/Desktop/3_1STKK.png" style="width:30.5mm;height:30.5mm;" class="addBorder"></img>
<img src="file:///Users/David/Desktop/4_1STKK.png" style="width:30.5mm;height:30.5mm;" class="addBorder"></img>
<img src="file:///Users/David/Desktop/5_1STKK.png" style="width:30.5mm;height:30.5mm;" class="addBorder"></img>

<tr>
<table>
</body>
</html>

Ant这是原来的html文件模板:

<html>
<style>
.addBorder {
border: 29.5px solid #ffffff;
}
</style>
<body>
<table>
<tr>

<img src="link" style="width:30.5mm;height:30.5mm;" class="addBorder"></img>

<tr>
<table>
</body>
</html>

我希望这是可能的。

编辑

我自己修复了它,这是我所做的:

for (int qr = 0; qr <= qrArray.count-1; qr++) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.png", [self documentPath], qrArray[qr]]];
NSString *costumeLink = [NSString stringWithFormat:@"<img src=\"%@\" style=\"width:30.5mm;height:30.5mm;\" class=\"addBorder\"></img>", url];
[qrPDFArray addObject:costumeLink];
}

NSString*htmlString = [NSString stringWithFormat:@"%@", [qrPDFArray description]];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@",\n " withString:@"\n"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\"," withString:@""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\"<" withString:@"<"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@">\"" withString:@">"];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@"(\n " withString:@""];
htmlString = [htmlString stringByReplacingOccurrencesOfString:@")" withString:@""];
NSLog(@"%@", htmlString);
NSString *tempFile = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"html"];
NSString* tempFileString = [NSString stringWithContentsOfFile:tempFile encoding:NSUTF8StringEncoding error:nil];
tempFileString = [tempFileString stringByReplacingOccurrencesOfString:@"HERE" withString:htmlString];

最佳答案

使用 NSString componentsSeparatedByString: 方法。

NSString *originalHTML = @"<table><tr><img link >aa</img><tr><table>";
NSArray *linkArray = @[@"aa",@"bb"]; //your links array
NSArray *allStrings = [originalHTML componentsSeparatedByString:@"<tr>"];
NSString *imgString = allStrings[1];
NSString *repeatedString=@"";
NSInteger i;
for(i=0;i<linkArray.count;i++){
NSString *linkString = [imgString stringByReplacingOccurrencesOfString:@"link" withString:linkArray[i]];
repeatedString = [repeatedString stringByAppendingString:linkString];
}
NSString *finalHTML = [NSString stringWithFormat:@"%@<tr>%@<tr>%@",allStrings[0],repeatedString,allStrings[2]];

NSLog(@"%@",finalHTML);

希望这有帮助。

关于html - 如何在 iOS 上的 html 文件中创建和替换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com