android - Objective-C - 在 UITextView 中获取简单的 HTML 源代码 - 没有 CSS 样式

标签 android html ios css objective-c

我想在 UITextView 中编辑 HTML 文本,该文本是在允许您使用 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];

返回带有CSS样式的HTML文本,如下所示:

<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 文本?

谢谢。

最佳答案

在 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:@""];

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

关于android - Objective-C - 在 UITextView 中获取简单的 HTML 源代码 - 没有 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40937370/

相关文章:

ios - ARkit - 相机位置和 3D 模型位置

ios - 我想如何将其他用户设置为 Apple Developer 程序中特定应用程序的管理员

java - 在 android 中使用预构建 JAR 中的资源

android - Android App Bundle 是否会自动从下载的 APK 加载类?

android - Android XML 中的异常

javascript - mailto 使用 javascript

html - 显示 : inline, 内联 block 和 block - Bootstrap

objective-c - 更好地控制 SenTesting Kit/OCUnit 中的测试套件

Android - 向上滑动面板(库)圆角

javascript - 如果 div 元素为空则隐藏它 (ASP.NET MVC)