【问题标题】:Get HTML in UITextView在 UITextView 中获取 HTML
【发布时间】:2013-09-04 07:39:28
【问题描述】:

我在UITextView中显示html by:

[self.textView setValue:@"<b>Content</b>" forKey:@"contentToHTMLString"];,

编辑UITextView中的内容后,我想获取包含html的内容,所以我使用:

[self.textView valueForKey:@"contentToHTMLString"]; 但是应用崩溃了,如何解决?

【问题讨论】:

  • 为什么不使用 UIWebView 而不是 UItextView..?
  • 我构建了类似邮件应用程序的邮件客户端,使用 UIWebview 撰写邮件无法使用光标位置滚动。

标签: ios uitextview


【解决方案1】:

对于 ios 7,请使用以下代码。

NSString *htmlString = @"<h1>Header</h1><h2>Subheader</h2><p>Some <em>text</em></p><img src='http://blogs.babble.com/famecrawler/files/2010/11/mickey_mouse-1097.jpg' width=70 height=100 />";
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        textView.attributedText = attributedString;

【讨论】:

    【解决方案2】:

    根据Display html text in uitextview这个问题使用UIWebView

    因为 未记录 -[UITextView setContentToHTMLString:] 方法。应用将因使用未记录的方法而被拒绝。

    但你仍然想这样做,你可以看看这个链接可能会有所帮助:-

    https://github.com/AliSoftware/OHAttributedLabel

    https://www.cocoacontrols.com/controls/bctextview

    https://www.cocoacontrols.com/controls/egotextview

    要使用 UIWebview 执行此任务,您必须查看以下内容:-

    Rich-text-editing-sample-project

    【讨论】:

      【解决方案3】:
      NSString *htmlContentString = self.compnayInfoPageDictionary[@"tDescription"];
      
      [self.txtViewInfoPage setValue:htmlContentString forKey:@"contentToHTMLString"];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-07
        • 1970-01-01
        • 2011-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多