2015-11-12 6 views
7

これは質問Detecting taps on attributed text in a UITextView in iOSa previous answer of mineの補足的な質問です。キーボードが表示されているときにUITextViewの属性付きテキストのタップを検出する

私はXcode 7.1.1とiOS 9.1で次のコードを再テストしました。リンク先の答えに記載されている設定でうまく動作します。それは

enter image description here

編集可能と選択の両方であるようにUITextView設定が変更された場合

import UIKit 
class ViewController: UIViewController, UIGestureRecognizerDelegate { 

    @IBOutlet weak var textView: UITextView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Create an attributed string 
     let myString = NSMutableAttributedString(string: "Swift attributed text") 

     // Set an attribute on part of the string 
     let myRange = NSRange(location: 0, length: 5) // range of "Swift" 
     let myCustomAttribute = [ "MyCustomAttributeName": "some value"] 
     myString.addAttributes(myCustomAttribute, range: myRange) 

     textView.attributedText = myString 

     // Add tap gesture recognizer to Text View 
     let tap = UITapGestureRecognizer(target: self, action: Selector("myMethodToHandleTap:")) 
     tap.delegate = self 
     textView.addGestureRecognizer(tap) 
    } 

    func myMethodToHandleTap(sender: UITapGestureRecognizer) { 

     let myTextView = sender.view as! UITextView 
     let layoutManager = myTextView.layoutManager 

     // location of tap in myTextView coordinates and taking the inset into account 
     var location = sender.locationInView(myTextView) 
     location.x -= myTextView.textContainerInset.left; 
     location.y -= myTextView.textContainerInset.top; 

     // character index at tap location 
     let characterIndex = layoutManager.characterIndexForPoint(location, inTextContainer: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil) 

     // if index is valid then do something. 
     if characterIndex < myTextView.textStorage.length { 

      // print the character index 
      print("character index: \(characterIndex)") 

      // print the character at the index 
      let myRange = NSRange(location: characterIndex, length: 1) 
      let substring = (myTextView.attributedText.string as NSString).substringWithRange(myRange) 
      print("character at index: \(substring)") 

      // check if the tap location has a certain attribute 
      let attributeName = "MyCustomAttributeName" 
      let attributeValue = myTextView.attributedText.attribute(attributeName, atIndex: characterIndex, effectiveRange: nil) as? String 
      if let value = attributeValue { 
       print("You tapped on \(attributeName) and the value is: \(value)") 
      } 

     } 
    } 
} 

しかし、これはキーボードが表示されます。キーボードが表示された後、タップイベントハンドラは呼び出されなくなりました。キーボードが表示されている間に属性付きテキストのタップを検出するには何ができますか?

更新

ここのコードは(答えにコメントで、私は上記にリンクされている)のObjective-Cで働いていたスウィフト、もともとこの質問をした者であるが。ですから、SwiftかObjective-Cのいずれかで答えを受け入れることは嬉しいです。あなたのUITextViewのコントローラーで

+0

私は客観的なCを使用してタップされた文字列を取得したい、あなたはそのコードを書く方法を知っていますか? – Eleanor

+0

@Liu - [iOSのUITextViewで属性付きテキストのタップを検出する](http://stackoverflow.com/questions/19332283/detecting-taps-on-attributed-text-in-a-uitextview)の質問は既に見ましたか? -in-ios)? Objective-Cにはいくつかの回答があります。 – Suragch

+0

ええ、私は特定のテキストを一度だけタップしたい、言い換えれば、単語をタップすると、その単語から属性を削除したいので、それを2回タップすることはできませんでした。それに、あなたは何か考えましたか? – Eleanor

答えて

0

、あなたはまた、あなたの帰属テキストの「タップ範囲」でなければなりませんのTextViewのselectedRangeにアクセスすることができます。このメソッド内メソッド

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{ 
} 

を上書きし、UITextViewDelegateを実装することができます。次に、必要に応じてtrue/falseを返します。

関連する問題