简体   繁体   English

在UITextView中创建可点击的文本部分

[英]Creating a tappable text section inside a UITextView

I have the following problem, I have a textview that says something like this "By tapping the "I agree" button, I certify that I have read and agree to the Online Terms and Conditions" 我遇到以下问题,我的文本视图中显示类似“通过点击“我同意”按钮,我证明我已阅读并同意在线条款和条件”

what I want to do is to add a tap gesture recognizer to the part that says "Online Terms and Conditions" and call a method when the user taps this text section, if the user clicks in any other section of the text nothing should happen. 我想做的是向“在线条款和条件”部分添加轻击手势识别器,并在用户单击此文本部分时调用方法,如果用户单击文本的任何其他部分,则不会发生任何事情。

How can I do this? 我怎样才能做到这一点? (I can't post any code due to the NDA I signed) :( I hope I can still be helped. (由于签署了NDA,我无法发布任何代码):(希望我能得到帮助。

Thanks in advance. 提前致谢。

You can use attributed string with the tag NSLinkAttributeName set for the range of tappable text. 您可以将属性字符串与标记为NSLinkAttributeName的标签NSLinkAttributeName使用,以设置可点击文本的范围。 Then in the textView delegate implement 然后在textView委托中执行

optional func textView(_ textView: UITextView, shouldInteractWith URL:URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool

Note that your text field must be selectable and non-editable for this to work. 请注意,您的文本字段必须是可选且不可编辑的,此功能才能起作用。

Reference: https://developer.apple.com/reference/uikit/uitextviewdelegate/1649337-textview 参考: https : //developer.apple.com/reference/uikit/uitextviewdelegate/1649337-textview

Prior to iOS 10 you can use: 在iOS 10之前,您可以使用:

optional func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool`

Reference: https://developer.apple.com/reference/uikit/uitextviewdelegate/1618606-textview 参考: https : //developer.apple.com/reference/uikit/uitextviewdelegate/1618606-textview

If you need to support both use the @available api - put @available(iOS, deprecated: 10.0) before pre-iOS 10 version and @available(iOS 10.0, *) before the one for iOS 10. That way you will avoid compile-time errors. 如果需要同时支持两者,请使用@available @available(iOS, deprecated: 10.0)在iOS 10之前的版本之前将@available(iOS, deprecated: 10.0)在iOS 10之前的版本@available(iOS 10.0, *) 。这样一来,您就可以避免编译时间错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM