2016-07-15 79 views
0

我有一个UITextView,它显示评论文本的css风格的内容。基本上,有一个htmlString有些风格。UITextView应该避免删除html标记

NSMutableAttributedString *attributedStringForComment = [[NSMutableAttributedString alloc] initWithData:[formatedStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

其中显示与UITextView上的CSS样式的内容。

现在,如果有人编辑文本,然后我们通过删除CSS样式来隐藏注释部分(因此用户不能删除其他人的评论),然后通过添加用于删除/删除颜色的属性来将样式应用于NSMutableAttributedString /添加词。

问题: 当用户完成编辑部分我想我的字符串与编辑文本作为HTML与评论的CSS样式。 当我们将NSMutableAttributedString应用于某种风格时,它将删除html标记和css风格。

任何技巧/技巧将帮助我达成解决方案。

+0

我没有理解一切。你有一些例子,你如何应用效果? – Larme

+0

@Larme我的要求是当我编辑textview的内容,然后编辑内容标签应该出现分别添加和删除任何单词。我可以通过应用属性样式来做到这一点,但我需要以HTML格式发布编辑后的内容,因为服务器需要HTML格式的内容。 –

回答

0
NSAttributedString *yourHTMLSourceCodeString = textView.attributedText; 
NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}; 
NSData *htmlData = [yourHTMLSourceCodeString dataFromRange:NSMakeRange(0, yourHTMLSourceCodeString.length) documentAttributes:documentAttributes error:NULL]; 
NSString* body = [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding]; 
+0

代码应附有详细描述,以及它为什么是正确的解决方案。 – sorak