kAi*_*AiN 1 uitextview uiviewcontroller ios swift
我正在使用 aTextView并且我想在用户接受条款和条件以及隐私政策的文本中创建两个不同的链接。
此外,我需要每个链接打开不同的 UIViewController.
谁能帮我举个例子来理解如何实现这一目标?
我需要了解如何创建两个超链接以及如何以两种不同的方式打开它们 ViewControllers
感谢大家为我提供的任何帮助
例如......我想得到一个类似于这个的 TextView
您可以使用以下 UITextView 委托方法和在swift 5.1上测试的属性字符串:
let attributedString = NSMutableAttributedString(string: "By continueing you agree terms and conditions and the privacy policy")
attributedString.addAttribute(.link, value: "terms://termsofCondition", range: (attributedString.string as NSString).range(of: "terms and conditions"))
attributedString.addAttribute(.link, value: "privacy://privacypolicy", range: (attributedString.string as NSString).range(of: "privacy policy"))
textView.linkTextAttributes = [ NSAttributedString.Key.foregroundColor: UIColor.blue]
textView.attributedText = attributedString
textView.delegate = self
textView.isSelectable = true
textView.isEditable = false
textView.delaysContentTouches = false
textView.isScrollEnabled = false
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
if URL.scheme == "terms" {
//push view controller 1
return false
} else if URL.scheme == "privacy"{
// pushViewcontroller 2
return false
}
return true
// let the system open this URL
}
Run Code Online (Sandbox Code Playgroud)
如果用户点击或长按 URL 链接,则 UITextView 调用此函数。此方法的实现是可选的。默认情况下,UITextview 打开那些负责处理 URL 类型并将 URL 传递给它们的应用程序。您可以使用此方法触发替代操作
| 归档时间: |
|
| 查看次数: |
700 次 |
| 最近记录: |