Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios7 NSTextAttachment和touch事件_Ios7_Uitextview_Textkit - Fatal编程技术网

Ios7 NSTextAttachment和touch事件

Ios7 NSTextAttachment和touch事件,ios7,uitextview,textkit,Ios7,Uitextview,Textkit,我有一个处于编辑模式的UITextView,几乎没有图像作为NSTextAttachment。我想截取这些上的触摸事件 我已在UITextView上设置了一个委托,但从未调用textView:shouldInteractWithTextAttachment:inRange:中的TextAttachment:inRange 从论坛上看,UITextView的可编辑属性似乎应该是NO,这样才能起作用,但根据官方文档,这是不可能的 编辑器如下所示: 正如您所提到的,textView:shouldInt

我有一个处于编辑模式的UITextView,几乎没有图像作为NSTextAttachment。我想截取这些上的触摸事件

我已在UITextView上设置了一个委托,但从未调用textView:shouldInteractWithTextAttachment:inRange:中的TextAttachment:inRange

从论坛上看,UITextView的可编辑属性似乎应该是NO,这样才能起作用,但根据官方文档,这是不可能的

编辑器如下所示:

正如您所提到的,textView:shouldInteractWithTextAttachment:inRange:不适用于可编辑文本视图。解决此问题的一种方法是实现您自己的UITapGestureRecognitor,并执行以下操作:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    if (self.state == UIGestureRecognizerStateFailed) return;

    UITouch *touch = [touches anyObject];
    UITextView *textView = (UITextView*) self.view;
    NSTextContainer *textContainer = textView.textContainer;
    NSLayoutManager *layoutManager = textView.layoutManager;

    CGPoint point = [touch locationInView:textView];
    point.x -= textView.textContainerInset.left;
    point.y -= textView.textContainerInset.top;

    NSUInteger characterIndex = [layoutManager characterIndexForPoint:point inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:nil];

    if (characterIndex >= textView.text.length)
    {
        self.state = UIGestureRecognizerStateFailed;
        return;
    }

    _textAttachment = [textView.attributedText attribute:NSAttachmentAttributeName atIndex:characterIndex effectiveRange:&_range];
    if (_textAttachment)
    {
        return;
    }
    _textAttachment = nil;
}
然后将该手势识别器添加到文本视图中,当该手势被识别时,您会要求输入_textAttachment值

请记住,characterIndexForPoint:inTextContainer:fractionOfDistanceBetweenInsertionPoints:返回最近的字符索引。您可能需要检查点是否在附件中,具体取决于您计划执行的操作。

如您所述,textView:shouldInteractWithTextAttachment:inRange:不适用于可编辑文本视图。解决此问题的一种方法是实现您自己的UITapGestureRecognitor,并执行以下操作:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    if (self.state == UIGestureRecognizerStateFailed) return;

    UITouch *touch = [touches anyObject];
    UITextView *textView = (UITextView*) self.view;
    NSTextContainer *textContainer = textView.textContainer;
    NSLayoutManager *layoutManager = textView.layoutManager;

    CGPoint point = [touch locationInView:textView];
    point.x -= textView.textContainerInset.left;
    point.y -= textView.textContainerInset.top;

    NSUInteger characterIndex = [layoutManager characterIndexForPoint:point inTextContainer:textContainer fractionOfDistanceBetweenInsertionPoints:nil];

    if (characterIndex >= textView.text.length)
    {
        self.state = UIGestureRecognizerStateFailed;
        return;
    }

    _textAttachment = [textView.attributedText attribute:NSAttachmentAttributeName atIndex:characterIndex effectiveRange:&_range];
    if (_textAttachment)
    {
        return;
    }
    _textAttachment = nil;
}
然后将该手势识别器添加到文本视图中,当该手势被识别时,您会要求输入_textAttachment值


请记住,characterIndexForPoint:inTextContainer:fractionOfDistanceBetweenInsertionPoints:返回最近的字符索引。根据您的计划,您可能需要检查该点是否在附件中。

谢谢,处理触摸开始:。。在我的自定义UITextView中,这似乎是一种可行的方法。仅供参考,我在UITextView中开始触摸时遇到了一些问题。它并不总是被呼叫。谢谢,处理触摸开始:。。在我的自定义UITextView中,这似乎是一种可行的方法。仅供参考,我在UITextView中开始触摸时遇到了一些问题。它并不总是被叫来。