【问题标题】:why do interactive parts of UITextview require a long press?为什么 UITextview 的交互部分需要长按?
【发布时间】:2016-10-13 09:39:19
【问题描述】:

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

1- 它需要长按

2- 并强制我选择文本

知道如何解决这个问题吗?

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

【问题讨论】:

    标签: swift uibutton uilabel uitextview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

          let attributedString = NSMutableAttributedString(string: "Press this link text")
          attributedString.addAttribute(NSLinkAttributeName, value: "http://www.google.com", range: NSRange(location: 6, length: 14))
      
          let linkAttributes = [
              NSForegroundColorAttributeName: UIColor.greyishBrownColor(),
              NSUnderlineColorAttributeName: UIColor.greyishBrownColor(),
              NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue
          ]
      
          agreementsTextView.delegate = self
          agreementsTextView.attributedText = attributedString
          agreementsTextView.linkTextAttributes = linkAttributes
      
      
          // Delegate Method
          func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
          if URL.absoluteString == "http://www.google.com" || URL.relativePath == "http://www.google.com" {
              print("Tapped")
          }
      
          return false
      }
      

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

      【讨论】:

      • 这个和我做的一样,也是需要长按,快按不行。
      • 不需要长按,我使用的项目很少,工作没有任何问题。
      • 在模拟器上确实需要长按,我的意思是,快速轻按不起作用,请告诉我您在模拟器和设备上的体验?
      • 对不起,你是对的,在模拟器上快速点击不起作用,但在真实设备上,工作。我刚刚测试过
      • 你用的是什么设备?什么iOS?你用的是什么 Xcode?​​span>
      猜你喜欢
      • 2011-12-29
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      相关资源
      最近更新 更多