我想在 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/