我目前正在使用Xamarin在我的iOS应用程序中实现Web视图。我的webView是一个WkWebView。我的问题是,无论何时传入的文本都有新行,都无法显示。但是,在我的浏览器(chrome)中测试我的功能以及Safari我发现它执行得很好。我做了一些搜索,我也尝试用\ r \ n替换\ n字符,但这并没有解决我的问题。我错过了什么?
C#:
private void BuildText(FormEntries entry, FormResponseAnswers formAnswers) {
string function = "buildText('" + entry.Text + "', '" + formAnswers.Answer + "');";
var javaScriptCmd = (NSString)function;
webView.EvaluateJavaScript(javaScriptCmd, null);
}
formAnswers.Answer导致问题是:
"Hello world from the device, I do not know how well this will display our data at all. But we will see how this works. I wonder, if I were to add enter keys will it work?\n\n\nI kinda doubt it. ";
JS:
function buildText(entryText, answer) {
var answerAreaDiv = document.getElementById('answerArea');
var holder = document.createElement('div');
holder.classList.add('holder');
var entryLabel = document.createElement("label");
entryLabel.textContent = entryText + ':';
var answerLabel = document.createElement("label");
answerLabel.innerText = answer;
holder.appendChild(entryLabel);
holder.appendChild(answerLabel);
answerAreaDiv.appendChild(holder);
}
HTML:
<body>
<div id="answerArea">
</div>
</body>
答案 0 :(得分:1)
除非您明确要求,否则HTML元素不会换行;诸如\n
之类的换行符被视为空格。请尝试插入HTML,以便您可以使用<br>
替换换行符:
answerLabel.innerHTML = answer.replace(/\n/g, '<br>');
示例:
document.body.appendChild(document.createElement('label')).innerHTML = `first line
second line`.replace(/\n/g, '<br>');
&#13;