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.
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
-
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