我试图在可点击链接的UITextview中显示一个归因的字符串.我创建了一个简单的测试项目,看看我在哪里错了,仍然无法弄清楚.我尝试启用用户交互并设置了shouldInteractWithURLs委托方法,但仍然无法使用.这是我的代码(对于只包含textview的视图控制器)
@IBOutlet weak var textView: UITextView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. let string = "Google" let linkString = NSMutableAttributedString(string: string) linkString.addAttribute(NSLinkAttributeName,value: NSURL(string: "https://www.google.com")!,range: NSMakeRange(0,string.characters.count)) linkString.addAttribute(NSFontAttributeName,value: UIFont(name: "HelveticaNeue",size: 25.0)!,string.characters.count)) textView.attributedText = linkString textView.delegate = self textView.selectable = true textView.userInteractionEnabled = true }
这里是我实现的委托方法:
func textViewShouldBeginEditing(textView: UITextView) -> Bool { return false } func textView(textView: UITextView,shouldInteractWithURL URL: NSURL,inRange characterRange: NSRange) -> Bool { return true }
这还不行.我搜索了这个话题,没有任何帮助.非常感谢提前.