在目标c中的wkwebview套件中更改<br> <div>和<span>的边距

时间:2019-09-23 21:45:46

标签: javascript html objective-c wkwebview wkwebviewconfiguration

我想减少HTML字符串中的空间。 下面是代码正在使用

  NSString *headerString = @"<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></header>";
    headerString = [headerString stringByAppendingString:htmlstring];
    headerString = [headerString stringByReplacingOccurrencesOfString:@"<br><br/><br/><br><br/><br/>" withString:@"<br><br/>"];
    headerString = [headerString stringByReplacingOccurrencesOfString:@"<br><br/><br><br/>" withString:@"<br><br/>"];
 [webviewkit loadHTMLString:headerString baseURL:nil];

我也尝试过使用Javascript,但是我不确定到底应该传递什么Java脚本,它只会给出1行间距,而不是多行间距:

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
 [webView evaluateJavaScript:@"document.open();document.close()" completionHandler:^(id _Nullable stringresult, NSError * _Nullable error) {
        NSLog(@"result=>%@ error->%@",stringresult,error);
    }];
}

但是我无法正确删除间距。

<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></header><div><span style="font-size: 16px;"><span style="font-size: 16px; font-family: Arial;">Hello and welcome to XYZ session!&nbsp;&nbsp;</span><br><br/><span style="font-family: Arial;">Over the next three Hello world.&nbsp; Spacing takes alot of spaces over here due to span , br and div tags.</span><br><br/><span style="font-family: Arial;">Thank you again for agreeing to participate and helping the discussion....</span><br><br/><span style="font-family: Arial;">Over here,</span><br><br/></span></div><br/><ul><br/>    <li><span style="font-size: 16px;"><span style="font-family: Arial;">I will be posting questions over here in stack overflow&nbsp;</span><br><br/>    </span></li><br/>    <li><span style="font-size: 16px;"><span style="font-family: Arial;">Throughout the day I will read everyone's responses and sometimes respond with my own follow-up questions.&nbsp;</span><br><br/>    </span></li><br/>    <li><span style="font-size: 16px;"><span style="font-family: Arial;">Please be i request to provide the code in objective c</span><br><br/>    </span></li><br/>    <li><span style="font-size: 16px;"><span style="font-family: Arial;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. &nbsp;</span><br><br/>    </span></li><br/>    <li><span style="font-size: 16px;"><span style="font-family: Arial;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. </span><br><br/>    </span></li><br/>    <li><span style="font-size: 16px; font-family: Arial;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. </span></li><br/></ul>

1 个答案:

答案 0 :(得分:1)

我认为您可能对<br>标签有些困惑。 <br><br />是自动关闭/无效元素/空标记:

https://www.w3schools.com/html/html_elements.asp(在空白html部分下) http://xahlee.info/js/html5_non-closing_tag.html

  

<br>是一个没有结束标记的空元素(<br>标记定义了一个换行符):

因此,当您在<br><br />中潜行时,实际上是在两个换行符中潜行。从您的描述中听起来好像您只想换行,所以您将要删除其中之一。