为什么UITextview的交互式部分需要长按?

时间:2016-10-13 09:39:20

标签: swift uibutton uilabel uitextview

我需要让文本的一部分互动,这是我到目前为止所做的截图,这很好,但问题是

1-它需要长时间点击 2-并强迫我选择文字

关于如何解决这个问题的任何想法?

clickable uilabel

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
    }
}

2 个答案:

答案 0 :(得分:1)

我不知道如何解决您的具体问题,但我想您最好继承UILabel而不是UITextView。此外,尝试不重新发明轮子,使用TTAttributedLabel或类似的库来做你想要的。

答案 1 :(得分:1)

我解决了这个问题,如下所示,

    this.insertMliveResponse = function(data){
        var defer=$q.defer();
        var requestURL='/mlive-portlet/rest/mliveResponseService/insertmLiveResponse';
        httpRequest(requestURL,data).then(function(data){
                defer.resolve(data.data);
        },function(data){
            defer.reject(data.data);
        })
        return defer.promise;
    }

范围可能有误,这应该可以解决您的问题