Ios UITextView href链接不工作
这是要显示的文本:Ios UITextView href链接不工作,ios,swift,uitextview,nsattributedstring,Ios,Swift,Uitextview,Nsattributedstring,这是要显示的文本: © Gregory A. Dunbar <a href=\"http://gregoryadunbar.com\" rel=\"nofollow\">gregoryadunbar.com</a>\n 注意:所有这些都在自定义视图中。 以下是回应: 所以我这里有两个问题: 文本未完全显示 链接不可点击 默认情况下,阻止使用http而不是https的链接。您需要在info.plist <key>NSAppTransportSecurit
© Gregory A. Dunbar <a href=\"http://gregoryadunbar.com\" rel=\"nofollow\">gregoryadunbar.com</a>\n
注意:所有这些都在自定义视图中。
以下是回应:
所以我这里有两个问题:
默认情况下,阻止使用
http
而不是https
的链接。您需要在info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>gregoryadunbar.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
如果未设置UITextView
的委托,默认情况下应该可以使用
如果您的链接包装在a href
标记中,您可以像这样剥离链接中的所有HTML
标记:
let link = "A. Dunbar <a href=\"http://gregoryadunbar.com\" rel=\"nofollow\">gregoryadunbar.com</a>\n"
let linkWithoutHtmlTags = link.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) // gregoryadunbar.com
let link=“A.Dunbar\n”
让linkwithout htmltags=link.replacingOccurrences(of:“]+>”,with:”,选项:。正则表达式,范围:nil)//gregoryadanbar.com
这里只有两点:1)我认为这不会检测到ahref标记2)文本是dynamic@Nitish1.不会检测到a href
标记,因为这样就不需要此标记,任何HTML
标记都不需要2。文本可以是动态的,range(of:)
将在您的文本中找到链接的范围在我的情况下,链接总是以href的形式出现。您是否可以打印textViewCaption.attributedText
?我想说你看不到它的开头,因为默认情况下文本是黑色的,你的背景色也是黑色的。这应该可以解释“未完全显示”的原因。是否调用委托方法?您需要self.addSubview(textViewCaption)
,什么是self
?是否启用用户对自身的交互?你能检查一下视图层次结构吗()它上面有子视图吗?
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>gregoryadunbar.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
let text = "your full string including link gregoryadunbar.com"
let range = text.range(of: "gregoryadunbar.com")
let attributedText = NSMutableAttributedString(string: text)
attributedText.addAttribute(.link, value: "http://gregoryadunbar.com", range: range)
attributedText.addAttribute(.font, value: someFont, range: Range(location: 0, length: text.characters.count))
attributedText.addAttribute(.foregroundColor, value: UIColor.white range: Range(location: 0, length: text.characters.count))
let link = "A. Dunbar <a href=\"http://gregoryadunbar.com\" rel=\"nofollow\">gregoryadunbar.com</a>\n"
let linkWithoutHtmlTags = link.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) // gregoryadunbar.com