I need to make part of a text interactive, this is a screenshot of what I made so far, which is good, but the problems are
1- it requires a long tap
2- and force me to have the text selectable
any idea about how to solve this?
import UIKit
class ViewController: UIViewController, UITextViewDelegate
{
@IBOutlet weak var textView: UITextView!
override func viewDidLoad()
{
super.viewDidLoad()
let attributedString = NSMutableAttributedString(string: "text with a link", attributes: nil)
attributedString.setSubstringAsLink(substring: "link", linkURL: "CUSTOM://WHATEVER")
let linkAttributes: [String : AnyObject] = [NSForegroundColorAttributeName : UIColor.redColor(), NSUnderlineColorAttributeName : UIColor.redColor(), NSUnderlineStyleAttributeName : NSUnderlineStyle.StyleSingle.rawValue]
textView.linkTextAttributes = linkAttributes
textView.attributedText = attributedString
textView.selectable = true
textView.editable = false
textView.userInteractionEnabled = true
textView.delegate = self
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool
{
if URL == "CUSTOM://WHATEVER"
{
print("????")
}
else
{
print("!!!!")
}
return true
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension NSMutableAttributedString
{
public func setSubstringAsLink(substring substring: String, linkURL: String) -> Bool
{
let range = self.mutableString.rangeOfString(substring)
if range.location != NSNotFound
{
self.addAttribute(NSLinkAttributeName, value: linkURL, range: range)
return true
}
return false
}
}
I don't know how to solve your specific issues but I guess you are better of subclassing UILabel instead of UITextView. Also, try to not re-invent the wheel, use TTAttributedLabel or similar libraries to do what you want.