2016-10-13 35 views
0

我需要做一個文本的一部分互動,這是我迄今爲止所取得的截圖,這是很好的,但問題是爲什麼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 
    } 
} 

回答

1

我不知道如何解決您的具體問題,但我想你是子類,而不是UITextView中的UILabel的更好。另外,儘量不要重新發明輪子,使用TTAttributedLabel或類似的庫來做你想做的事。

1

我解決了這個問題像下面,

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 
} 

範圍可能是錯的,這應該解決您的問題

+0

這是我做的一樣,它也需要長按,快速點擊不要」工作。 – DeyaEldeen

+0

不需要長時間點擊,我使用的項目很少,而且沒有任何問題。 – Abdulkerim

+0

在模擬器上它確實需要長時間點擊,我的意思是,快速點擊不起作用,請告訴我關於您在模擬器和設備上的體驗嗎? – DeyaEldeen

相關問題