显示键盘时在UITextView中检测对属性文本的轻击 [英] Detecting taps on attributed text in a UITextView while the keyboard is showing

查看:35
本文介绍了显示键盘时在UITextView中检测对属性文本的轻击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是

然后将导致键盘显示.显示键盘后,不再调用tap事件处理程序.在键盘显示时如何检测属性文本上的轻敲?

更新

尽管这里的代码在Swift中,但是最初问这个问题的人(在上面链接到我的答案的评论中)是与Objective-C一起工作的.因此,我很高兴接受Swift或Objective-C的答案.

解决方案

在UITextView的控制器中,您可以实现 UITextViewDelegate ,然后覆盖方法

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

在此方法内,您可以访问textView的selectedRange,它也应该是属性文本的点击范围".然后根据您的需要返回true/false.

This is a supplemental question to a previous answer of mine to the question Detecting taps on attributed text in a UITextView in iOS.

I retested the following code with Xcode 7.1.1 and iOS 9.1 and it works fine with the setup described in the answer I linked to.

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

        }
    }
}

However, if the UITextView settings are changed so that it is both editable and selectable

then this will cause the keyboard to display. After the keyboard is shown, the tap event handler no longer gets called. What can be done to detect taps on attributed text while the keyboard is showing?

Update

Although the code here is in Swift, the person who originally asked this question (in a comment to the answer I linked to above) was working with Objective-C. So I would be glad to accept an answer in either Swift or Objective-C.

解决方案

In your UITextView's controller,you can implement UITextViewDelegate , Then override method

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

Inside this method you can access the textView's selectedRange,which should also be the "tap range" of your attributed text. Then return true / false depending on your needs.

这篇关于显示键盘时在UITextView中检测对属性文本的轻击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆