Skip to content

How to detect the tapped text in CKTextComponent? #246

Closed

Description

@tranngoclinh88

I'm using CKUIControlEventTextViewDidTapText to handle event tap on text in CKTextComponent.
Here is my code:

CKComponent *nameComponent = [CKTextComponent newWithTextAttributes:{ headerTitleAttributedString } viewAttributes:{CKComponentActionAttribute(@selector(doSomething:), CKUIControlEventTextViewDidTapText) } accessibilityContext:{}];

  • (void) doSomething:(id)sender {}

When I tap on an attributed string of CKTextComponent, the sender is always whole text in component. I only want to know the tapped text of component. Please tell me how to detect it.
Thanks!

Activity

  1. ocrickard commented on Jun 11, 2015

    @ocrickard
    Contributor

    You would grab the renderer object off of the CKTextComponentView, and then ask for the text checking result at the point the user touched (https://github.com/facebook/componentkit/blob/master/ComponentTextKit/TextKit/CKTextKitRenderer%2BTextChecking.h#L27).

    Also, please see issue #74

  2. tranngoclinh88 commented on Jun 11, 2015

    @tranngoclinh88
    Author

    @ocrickard
    I think it's my bad explain. I had implemented follow issue #74. I can see the text username highlight when I touch on it but the sender is the CKTextComponent with all attributed string (not only the username).
    Example:

    • All text is "abc had do something".
    • I touch on text "abc" and it's highlight.
    • The function 'doSomething:' is fired but the sender is the CKTextComponent "abc had do something."

    What's wrong with my code in viewAttributes:
    {CKComponentActionAttribute(@selector(doSomething:), CKUIControlEventTextViewDidTapText) }

    How can I get the touched text "abc"?

  3. ocrickard commented on Jun 12, 2015

    @ocrickard
    Contributor

    You would change your doSomething: method to look something like this instead:

    - (void)doSomething:(CKTextComponent *)sender withEvent:(UIEvent *)event
    {
      CKTextComponentView *view = self.component.viewContext.view; // Assumes the tap is being handled by your CKComponentController
      CKTextKitRenderer *renderer = view.renderer;
      UITouch *touch = [[event allTouches] anyObject];
      CGPoint point = [touch locationInView:view];
      NSTextCheckingResult *result = [renderer textCheckingResultAtPoint:point];
      // Profit.
    }
    
  4. tranngoclinh88 commented on Jun 15, 2015

    @tranngoclinh88
    Author

    Thank @ocrickard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions