o zaman textView delegate metodlarından biri olan shouldInteractWithURL kullanılarak aşağıdaki örnekte verdiğim gibi birşeyler yapabilirsiniz mesela.
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let myString:String = "asdasdasd asd asdasd asda sdasdasd asdasd asdasd www.asd.com asdasd asdasd www.sdf.com"
let mainAttributedString:NSMutableAttributedString = NSMutableAttributedString()
let textFont:UIFont = UIFont(name: "Avenir-Heavy", size: 20)!
let textParagraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle()
textParagraphStyle.alignment = NSTextAlignment.Center
let textAttributeDict = [NSFontAttributeName:textFont, NSForegroundColorAttributeName:UIColor.blackColor(), NSParagraphStyleAttributeName:textParagraphStyle]
let textAttributedString:NSMutableAttributedString = NSMutableAttributedString(string: myString, attributes: textAttributeDict)
mainAttributedString.appendAttributedString(textAttributedString)
let hyperTextAttributeDict = [NSFontAttributeName:textFont, NSForegroundColorAttributeName:UIColor.redColor(), NSParagraphStyleAttributeName:textParagraphStyle]
var textView = UITextView(frame: self.view.frame)
textView.delegate = self
textView.linkTextAttributes = hyperTextAttributeDict
textView.dataDetectorTypes = UIDataDetectorTypes.Link
textView.backgroundColor = UIColor.clearColor()
textView.font = textFont
textView.textColor = UIColor.blackColor()
textView.textAlignment = NSTextAlignment.Justified
textView.editable = false
textView.userInteractionEnabled = true
textView.selectable = true
textView.scrollEnabled = true
textView.scrollsToTop = true
textView.attributedText = mainAttributedString
self.view.addSubview(textView)
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
let textViewString:NSString = textView.text as NSString
let rangedString:String = textViewString.substringWithRange(characterRange)
if rangedString == "www.sdf.com" {
println("do something else")
return false
}
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}