ios 在 UIWebview 中加载字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13343794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:10:57  来源:igfitidea点击:

Loading string in UIWebview

iphoneiosuiwebview

提问by Dev

How to load a string like below in UIWebview

如何在 UIWebview 中加载如下所示的字符串

"\n
\nEgestas rhoncus auctor ac. Risus parturient, mid ultrices nisi.\U00a0
\nAugue ac elementum duis aliquet dolor elementum cum?\U00a0
\nTristique, augue sit lorem adipiscing dis!\U00a0
\nNunc nunc ultricies pellentesque dis dictumst enim
\n
\n"

I am trying to load this same content in 5 webviews..But it is crashing for me..

我正在尝试在 5 个 webviews 中加载相同的内容..但它对我来说崩溃了..

回答by MANIAK_dobrii

You should use:

你应该使用:

[webView loadHTMLString:string baseURL:nil];

But note, that you won't preserve line breaks. To preserve line breaks you'll need to replace \n with <\br> (or any valid line breaking html), so this turns into:

但请注意,您不会保留换行符。要保留换行符,您需要将 \n 替换为 <\br> (或任何有效的换行符 html),所以这变成:

[webView loadHTMLString:[string stringByReplacingOccurrencesOfString:@"\n" withString:@"<br/>"] baseURL:nil];

回答by cwehrung

This is pretty simple As Below,

这很简单,如下所示,

 [self.webView loadHTMLString:htmlString baseURL:nil];

You can specify the baseURLto get relative paths working. Is this what you were looking for ? Or more general explanations about UIWebView ?

您可以指定baseURL使相对路径工作。这是您要找的吗?或者更一般的解释UIWebView ?

回答by Kamar Shad

Here is the Code

这是代码

 NSString* htmlString= @"hereyourSTring";
 [webView loadHTMLString:htmlString baseURL:nil];

this would load the String in WEBview.

这将在 WEBview 中加载字符串。

回答by Karan Dua

In Swift 3

在斯威夫特 3

let webView = UIWebView()
webView.loadHTMLString("htmlString", baseURL: nil)

But note, that you won't preserve line breaks. To preserve line breaks you'll need to replace \n with <\br> as done by @MANIAK_dobrii in above answer

但请注意,您不会保留换行符。要保留换行符,您需要将 \n 替换为 <\br> ,如@MANIAK_dobrii 在上述答案中所做的那样