Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

How to add event listener to part of a textfield

I need some pointers on making part of the text in a tableview's entry clickable. I can create the tableview, and rows containing the text from a twitter stream, and can isolate the twitter usernames. What I don't know is how to add an event listener to part of the text, not the whole thing.

Any advice is appreciated.

Many thanks.

— asked November 7th 2010 by Paul Taylor
  • eventlistener
  • iphone
  • tableview
2 Comments
  • It's very off-putting participating in this Q&A when, after providing a detailed solution, the poster gives no feedback on whether it was useful, whether it worked, or whether it was appreciated at all. Maybe I'm just old-fashioned!

    If you are still struggling with this issue, post more information. Otherwise, would you please close it by marking it as answered?

    — commented November 9th 2010 by Paul Dowsett
  • I do apologise Hal. Since I haven't been able to spend any time over the last two days on this, I haven't updated the answer. I am looking at it now, and assure you the question will wither be closed or marked as answer.

    — commented November 10th 2010 by Paul Taylor

2 Answers

  • I presume that the text that you want to make clickable will be different each time, and they will be all different sizes? Thus, although the label click event exposes the x and y coordinates of the click, I can't think of any way of knowing to what limits your dynamic text would extend.

    Therefore, the only way I can think of for you to achieve your aim is to use a separate label for the text that you want clickable.

    Hal

    — answered November 7th 2010 by Paul Dowsett
    permalink
    2 Comments
    • Hal,

      It's a twitter stream, so not only will the full text be different in each row, but the regexpd text will be different each time. A twitter mention does not appear as separate text, so this is not an option.

      Anyone else have any idea how to do this?

      — commented November 7th 2010 by Paul Taylor
    • It's an interesting question, but without sitting down and working through it, I can't think of a solution off-hand. Hopefully someone else will chime in.

      I note that you are using regexp to find the mention. Are you certain the Twitter API can't give you the information in a more manageable format by the mentions field? You may find the API Console useful.

      — commented November 8th 2010 by Paul Dowsett
  • Paul

    After some thought, I may have a solution:

    • split the tweet into an array, where each segment of the message and each of the mentions are in a separate element, so "This is my @username message that I sent to @username2 a while ago" returns an array containing 5 elements. Note that myArray.split() accepts regex, so use something like

      var thisTweet = "This is my tweet that I sent to @username but also mentioned @username2 when I sent it a while ago";
      thisTweet.split(/(@{1}\w+)/);
      
    • create a view that has the layout:'horizontal' property set at creation

    • use a loop to create a new label for each element in the array, and add each to the view
    • create eventlisteners for each @mention and attach them to the respective labels

    Note that this will currently only work on iOS, as there is a bug for android relating to horizontal layouts

    Hope this helps

    Hal

    — answered November 8th 2010 by Paul Dowsett
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.