【问题标题】:Objective-C - Get simple HTML source in UITextView - without CSS stylesObjective-C - 在 UITextView 中获取简单的 HTML 源代码 - 没有 CSS 样式
【发布时间】:2017-04-17 16:03:15
【问题描述】:

我想在 UITextView 中编辑 HTML 文本,此文本是在应用程序的 Web 版本中创建的,允许您使用 HTML 编辑器创建文本(编辑将使用 iOS 中的默认字体)

我得到了一个简单的 html 中要编辑的文本,如下所示:

<p><b>html</b> <u>html</u> <i>html</i></p>

但是在编辑之后,我在 UITextView 中得到了带有代码的 html 文本:

NSDictionary *exportParams = @{NSDocumentTypeDocumentAttribute:  NSHTMLTextDocumentType};
NSData *htmlData = [_txtDescricao.attributedText dataFromRange:NSMakeRange(0, _txtDescricao.attributedText.length) documentAttributes:exportParams error:nil];
NSString *descHtml = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];

HTML 文本以 CSS 样式返回,如下所示:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Generator" content="Cocoa HTML Writer">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px 'Helvetica Neue'; color:      #000000; -webkit-text-stroke: #000000}
span.s1 {font-family: 'Helvetica Neue'; font-weight: bold; font-style: normal; font-size: 16.00pt; font-kerning: none}
span.s2 {font-family: 'Helvetica Neue'; font-weight: normal; font-style: normal; font-size: 16.00pt; font-kerning: none}
span.s3 {font-family: 'Helvetica Neue'; font-weight: normal; font-style: normal; font-size: 16.00pt; text-decoration: underline ; font-kerning: none}
span.s4 {font-family: 'Helvetica Neue'; font-weight: normal; font-style: italic; font-size: 16.00pt; font-kerning: none}
</style>
</head>
<body>
<p class="p1"><span class="s1">html</span><span class="s2"> </span><span  class="s3">html</span><span class="s2"> </span><span class="s4">html</span></p>
</body>
</html>

我想以简单的 HTML 格式(没有 CSS)在 UITextView 中获取 html 文本,作为初始文本。

虽然它是一种有效的 HTML 格式,但我需要简化格式的 HTML 文本(不带 CSS),因为我可以在 Android 版本的应用程序中编辑相同的文本,而在 Android 中 EditText 不接受带有 CSS 的 HTML 文本.

谁能帮我在不使用 CSS 的情况下获取 UITextView 中的 HTML 文本?

谢谢。

【问题讨论】:

  • 这个问题我看了好几遍,没看懂。到目前为止,我已经得到:您的应用程序呈现一个 UITextView,它允许用户输入简单的 html。 myTextView.text 将返回用户输入的任何内容。这就是你想要的吗?
  • myTextView.text 返回文本:“html html html”,我想得到这样的文本:

    html html html

标签: android html ios css objective-c


【解决方案1】:

在 iOS 中,您可以手动删除 html 字符串的 style 元素,如下所示:

NSString *htmlString = @"Your entire html text as a string";
NSRange firstRange = [htmlString rangeOfString:@"<style type=\"text/css\">"];
NSRange secondRange =  [htmlString rangeOfString:@"</style>"];
NSRange range = NSMakeRange(firstRange.location, firstRange.length+secondRange.length);
[htmlString stringByReplacingCharactersInRange:range withString:@""];

您可能需要稍微测试一下这段代码并让它为您工作,但这个一般应该是一个简单的解决方案。

【讨论】:

  • 但我不想只删除样式,我想删除它们,并将它们应用到标签中,例如:

    html html html

猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 2012-10-06
  • 1970-01-01
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
相关资源
最近更新 更多