迅捷:转换为NSAttributed字符串的HTML数据无法使链接在标签中可点击 [英] swift: The HTML data converted to NSAttributed string doesn't make the links clickable in label
问题描述
我有一个HTML格式的文本.我正在使用NSAttributed字符串的属性来解析它.它可以很好地解析文本并显示在标签上.但是,锚标记的解析不会使链接可单击.这是我用于解析的以下代码.
扩展字符串{var htmlToAttributedString:NSAttributedString吗?{警卫让数据=数据(使用:.utf8),否则{返回NSAttributedString()}做 {返回尝试NSAttributedString(数据:数据,选项:[.documentType:NSAttributedString.DocumentType.html,.characterEncoding:String.Encoding.utf8.rawValue],documentAttributes:无)} 抓住 {返回NSAttributedString()}}var htmlToString:字符串{返回htmlToAttributedString?.string?"}
当我运行应用程序并将其值赋予标签时:
text =< p>这是< a href = \" http://www.google.com \> Google主页</a></p>"simpleTextLabel.attributedText = text.htmlToAttributedString
iOS App上的输出如下所示,但单击时无反应:
这是 Google主页.
如何在野生动物园中将其打开?
从您所在的行开始:
simpleTextLabel.attributedText = text.htmlToAttributedString
我们可以假设 simpleTextLabel
是 UILabel
.
这是 UILabel
的基本行为,它是不可交互的".您可以在其上添加轻击手势,但会将其转换为 UIButton
.
有一些技巧可以使 UILabel
成为可能,找到确切被点击的位置,检查是否存在链接等.寻找"UILabel可点击":创建可点击的"; links在UILabel的NSAttributedString中?等.甚至还有一些第三方库.
我(以我的拙见)将其视为黑客".
WWDC 2018会议:TextKit最佳实践.在2:34,它说明了如果您需要与显示的文本进行交互,则首选 UITextView
而不是 UILabel
.
有一个专门用于此目的的 UITextViewDelegate
方法: textView(_:shouldInteractWith:in:interaction:)
请注意, When I run the app and give the value to the label as: The output on the iOS App looks like following but nothing happens on clicking : This is Google Home. How can I make it open in safari? From your line: We can assume that It's basic behavior from a There are some tricks to make it possible with a I (in my humble opinion) consider it as a "hack". There is a good WWDC 2018 Session: TextKit Best Practices. At 2:34, it explains that if you need to interact with a shown text, prefers There is a Note that there a small differences in the rendering of a Note also that according to the small modifications of a 这篇关于迅捷:转换为NSAttributed字符串的HTML数据无法使链接在标签中可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! UITextView
和 UILabel
的呈现方式略有不同.如果叠加它们,它们将不会具有相同的起点",布局会有所不同.但是,只需稍作更改,它就可以相同(例如:text = "<p>This is <a href=\"http://www.google.com\">Google Home</a></p>"
simpleTextLabel.attributedText = text.htmlToAttributedString
simpleTextLabel.attributedText = text.htmlToAttributedString
simpleTextLabel
is a UILabel
.UILabel
to not be "interactable". You can add a tap gesture on it, but it transform it as a UIButton
.UILabel
, find where exactly it has been tapped, check if there is a link, etc.
Looking for "UILabel Clickable": Create tap-able "links" in the NSAttributedString of a UILabel? etc. There are even a few third party libs. UITextView
over UILabel
.UITextViewDelegate
method just for that: textView(_:shouldInteractWith:in:interaction:)
UITextView
and a UILabel
. If you superpose them, they won't have the same "start point", the layout is a little different. However, with small changes, it can be the same (for instance: How can I make a UITextView layout text the same as a UILabel?).UITextView
into a UILabel
visual rendering, the user won't notice the difference (and that's in fact what matters, beside that using native methods of the UITextView
/UITextViewDelegate
make it easily understandable afterwards by another developer, or in a few months if you need to do a small modification).