ios7 - NSTextAttachment and touch events -
i have uitextview (in edit mode) few images (as nstextattachment). i'd intercept touch events on these.
i have set delegate on uitextview textview:shouldinteractwithtextattachment:inrange: never called.
(from forums seems uitextview's editable property should no work, not per official documentation)
the editor looks this:
as mentioned, textview:shouldinteractwithtextattachment:inrange: not meant work editable text views. way around implement own uitapgesturerecognizer , this:
- (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; } then add gesture recogniser text view , when gesture recognized ask _textattachment value.
bear in mind characterindexforpoint:intextcontainer: fractionofdistancebetweeninsertionpoints: returns nearest character index. might want check if point inside attachment depending on you're planning do.
Comments
Post a Comment