首页 > 解决方案 > 更改边距

在目标 c 中的 wkwebview 套件中

问题描述

我想减少 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>

标签: javascripthtmlobjective-cwkwebviewwkwebviewconfiguration

解决方案


我想你可能对<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 />实际上是在两个换行符中插入字幕。根据您的描述,听起来好像您只想要一个换行符,因此您需要删除其中一个。


推荐阅读