如何在NSAttributedString中创建可连接的链接

时间:2016-05-18 05:16:45

标签: ios swift

我正在努力使UITextView内容的某些部分可以点击。这是我的代码:

class ViewController: UIViewController,UITextViewDelegate {


@IBOutlet weak var definition:UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //self.loadHTML()
    self.makeList()
}

func makeList(){

    definition.delegate = self

    let attributedString = NSMutableAttributedString(string: "Want to learn iOS? You should visit the best source of free iOS tutorials!")
    attributedString.addAttribute(NSLinkAttributeName, value: "https://www.hackingwithswift.com", range: NSRange(location: 0, length: 5))
    definition.attributedText = attributedString
}



func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
    UIApplication.sharedApplication().openURL(URL)
    return true
}
}

这有些不起作用。你能帮我解决一下我失踪的事吗?

1 个答案:

答案 0 :(得分:5)

    definition.editable = false;
    definition.delegate = self;
希望可以帮助你。