在UITextview中添加链接属性的文字

    let termsAndPrivacyLabel = UITextView(frame: CGRect(x: 24/2, y: 0, width: width, height: height))
        let string = "By signing up, you agree to the Terms of Use & Privacy Policy."
        let text = NSMutableAttributedString(string: string, attributes: [NSFontAttributeName: UIFont.systemFontOfSize(11.6)])
        text.addAttribute(NSLinkAttributeName, value: "https://github.com/site/terms", range: NSMakeRange(32, 12))
        text.addAttribute(NSLinkAttributeName, value: "https://github.com/site/privacy", range: NSMakeRange(47, 14))
        termsAndPrivacyLabel.attributedText = text
        termsAndPrivacyLabel.autoresizingMask = .FlexibleWidth
        termsAndPrivacyLabel.backgroundColor = UIColor.clearColor()
        termsAndPrivacyLabel.contentInset.top = 3 // shows the top dot of text-selector bar
        termsAndPrivacyLabel.editable = false
        termsAndPrivacyLabel.scrollEnabled = false
        tableFooterView.addSubview(termsAndPrivacyLabel)

 点击该高亮的文字会自动跳转到链接界面

 

posted @ 2016-01-20 18:27  mogul  阅读(378)  评论(0编辑  收藏  举报