दिखा रहा है, जबकि UITextView में जिम्मेदार पाठ पर नल का पता लगाना प्रश्न a previous answer of mine पर यह एक पूरक प्रश्न है।कुंजीपटल
मैं Xcode 7.1.1 और iOS 9.1 के साथ निम्नलिखित कोड पुनर्परीक्षण और यह जवाब मैं से जुड़ा हुआ में वर्णित सेटअप के साथ ठीक काम करता है।
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)")
}
}
}
}
हालांकि, UITextView
सेटिंग्स को बदल रहे हैं इतना है कि यह दोनों संपादन योग्य और चयन
तो इस कुंजीपटल प्रदर्शित करने के लिए कारण होगा है। कीबोर्ड दिखाए जाने के बाद, टैप इवेंट हैंडलर को अब कॉल नहीं किया जाता है। कुंजीपटल दिखाए जाने पर जिम्मेदार पाठ पर नल का पता लगाने के लिए क्या किया जा सकता है?
अद्यतन
हालांकि कोड यहां स्विफ्ट, जो व्यक्ति मूल रूप से इस प्रश्न पूछा (उत्तर मैं ऊपर से जुड़ा हुआ के लिए एक टिप्पणी में) में है ऑब्जेक्टिव-सी के साथ काम कर रहा था। इसलिए मुझे स्विफ्ट या ऑब्जेक्टिव-सी में एक जवाब स्वीकार करने में खुशी होगी।
मैं स्ट्रिंग कि उद्देश्य सी का उपयोग कर उपयोग किया किया गया प्राप्त करना चाहते हैं, तो आप जानते हैं कि कैसे है कि कोड लिखने के लिए करते हैं? – Eleanor
@Liu - आप पहले से ही सवाल (http://stackoverflow.com/questions/19332283/detecting-taps-on-attributed-text-in-a-uitextview [आईओएस में एक UITextView में जिम्मेदार ठहराया पाठ पर नल का पता लगा रहा] देखा था -इन-ios)? इसमें कई जवाब हैं जो उद्देश्य-सी में हैं। – Suragch
हाँ, लेकिन मैं दूसरे शब्दों में केवल एक बार कुछ पाठ दोहन करने के लिए, जब मैं एक शब्द भी टैप करते हैं, मैं उस शब्द से विशेषता निकालना चाहते हैं, ताकि मैं इसे दो बार टैप नहीं कर सकता है, मैं कैसे नहीं पता था चाहते हैं उस पर, क्या आपको कोई विचार है? – Eleanor