2015-02-09 38 views
8

我对iOS开发非常陌生,现在正在研究接收某种JSON数据的应用程序。但是一些后端专家认为,如果用户直接从Word中复制信息并将其粘贴到信息系统中,则会更好。所以我坐在这里,试图在UITableView中创建一个可点击的链接。UITextView中的HTML格式

我解析来自网络的数据,并得到一个字符串格式如下:

F&uuml;r mehr Informationen klicken sie <a href="http://www.samplelink.com/subpage.php?id=8">here</a>. 

我已经尝试过一个UILabel,但经过一番研究,我现在使用的经常建议的UITextView。在Attributed Inspector中,我将它设置为“已分配文本”并启用了链接检测。现在文本显示为红色并且可点击。

现在的问题是,HTML标记和正确的(德语)字符集仍然丢失,我不知道如何以正确的方式显示它。

所显示的字符串被解析这样:

func showHTMLString(unformattedString: String) -> NSAttributedString{ 
    var attrStr = NSAttributedString(
     data: tmpString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, 
     options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
     documentAttributes: nil, 
     error: nil) 
    return attrStr! 
} 

如果我填与attrStr?.string TextView的格式显示在正确的方式,但该链接消失了为好。

任何建议如何以正确的方式格式化显示的字符串?

在此先感谢 AR4G4

+0

你能解释一下你想要什么,输出的样子?例如,您的预期输出是“这里有更多信息。”这里是一个可点击的链接? – 2015-02-09 18:27:48

+0

那正是我想要的 – a2hur 2015-02-09 20:34:15

回答

25

的问题还有就是你必须改变从NSUnicodeStringEncoding字符编码选项NSUTF8StringEncoding载入你的HTML你的正确方法。我想你应该创建一个字符串扩展只读属性计算到你的html代码转换成属性串:

的Xcode 8.3.1•斯威夫特3.1

extension Data { 
    var attributedString: NSAttributedString? { 
     do { 
      return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil) 
     } catch { 
      print(error) 
     } 
     return nil 
    } 
} 
extension String { 
    var data: Data { 
     return Data(utf8) 
    } 
} 

​​

你的情况

yourTextView.attributedText = htmlStringCode.data.attributedString 
+0

这似乎是要走的路,特别是如果你会做很多。 – 2015-02-09 18:35:33

+0

谢谢@Leonardo Savio Dabus,带着你的提示,现在一切正常!只是需要重新分配单元格,但链接和显示现在显示了我想要的。 – a2hur 2015-02-09 20:52:36

+0

欢迎您 – 2015-02-09 20:54:47

0

创建了你的属性串,那么您需要设置UITextViewattributedText财产是NSAttributedString本身,而不是属性串的string财产。

1

我会建议在UIWebView中显示HTML。它比使用UITextView更强大。有关更多信息,请参阅Display html text in uitextview

+0

也尝试过,并且也可以。 'webView.loadHTMLString(myString.string,baseURL:nil)'以正确的方式显示链接和编码。 但你说什么更强大?我认为在每个UITableViewCell中使用UIWebView可能会有点太多。但就像我说的,我是iOS Dev的新手,不知道,这是最好的练习。 – a2hur 2015-02-09 21:02:12

+0

它是一个真正的HTML渲染器,它更健壮。例如,它可以处理CSS样式表,它可以运行JavaScript等。我不确定你期望呈现什么样的HTML - 对于简单的事情,UITextView可能工作。 – tng 2015-02-09 22:55:40

+0

hey tng,webview似乎是更好的解决方案(加载速度更快),但我现在陷入困境,该链接在小型web视图中打开。在这里查找:[链接](http://stackoverflow.com/questions/2899699/uiwebview-open-links-in-safari)但我需要把shouldStartLoadWithRequest?现在创建一个'类CustomUIWebViewController:UIWebView,UIWebViewDelegate'并将该函数放在那里,但链接仍然在WebView中打开。 (我的代表太低,不能评论其他主题)谢谢 – a2hur 2015-02-10 18:24:44

2

检查IB中UITextView的属性。为了使链接正常工作,您必须检查Selectable

enter image description here

+0

jep,我之前尝试过很多变化。现在我可以选择并启用链接检测。 – a2hur 2015-02-09 20:54:27

0

您的版本非常接近开始。由于莱昂纳多萨维奥Dabus说你应该尝试NSUTF * StringEncoding。以下为我产生您的预期输出。正如他所说的,你可能想将它添加到字符串的扩展,如果你这样做了很多。

let theString = "F&uuml;r mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>." 
    let theAttributedString = NSAttributedString(data: str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!, 
               options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil) 
    theTextView.attributedText = atString 
+0

NSUTF16StringEncoded是正确的,而不是NSUTF8StringEncoding – 2016-04-06 11:06:23

0

我用来做另一种方式:

var someHtmlString = "F&uuml;r mehr Informationen klicken sie <a href=\"http://www.samplelink.com/subpage.php?id=8\">here</a>." 
let regex = try! NSRegularExpression(pattern: "<.*?>", options: [.CaseInsensitive]) 
let range = NSRange(location: 0, length: someHtmlString.characters.count) 
let htmlLessString: String = regex.stringByReplacingMatchesInString(someHtmlString, options: NSMatchingOptions(), range:range, withTemplate: "") 

最终结果 - > htmlLessString是

"F&uuml;r mehr Informationen klicken sie here." 
0

我有一个应用程序,有一个UITextView,我希望能够到从浏览器中粘贴HTML格式的文本,然后将其保存为数据库中的字符串(包含html格式),然后再次从数据库中检索它,并使用与第一次从数据库中复制的相同格式显示它e网站。 我管理这个通过使这两个扩展:

extension String 
{ 
    func getAttributedStringFromHTMLString() -> NSAttributedString 
    { 
     do { 
      let attributedString = try NSAttributedString(data: self.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil) 
      return attributedString 
     } catch { 
      print(error) 
      return NSAttributedString() 
     } 
    } 
} 

extension NSAttributedString 
{ 
    func getHTMLString() -> String 
    { 
     var htmlText = ""; 
     let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] 
     do { 
      let htmlData = try self.dataFromRange(NSMakeRange(0, self.length), documentAttributes:documentAttributes) 
      if let htmlString = String(data:htmlData, encoding:NSUTF8StringEncoding) { 
       htmlText = htmlString 
      } 
      return htmlText 
     } 
     catch { 
      print("error creating HTML from Attributed String") 
      return "" 
     } 
    } 
} 
1

我使用的代码为雨燕4:

var descriptionStr : String = String() //Dynamic text 

let regex = try! NSRegularExpression(pattern: "<.*?>", options: [.caseInsensitive]) 
     let range = NSRange(location: 0, length: descriptionStr.count) 
     let htmlLessString: String = regex.stringByReplacingMatches(in: descriptionStr, options: NSRegularExpression.MatchingOptions(), range:range, withTemplate: "") 
     textViewRef.text = htmlLessString