How to make URL in a text string clickable?

My app downloads a lot of texts from the net and shows them on the screen.

However, in these texts, there are some URL links (http://www.xyz.com/12345/…) in between.

How do I make these URLs clickable?

I mean when the user clicks on a URL, how can I get an event (and know which URL is clicked) in order to launch a web view for it?

It depends how you’re displaying the text. If you’re using a web view to show the content the links should work as normal, your app won’t get an event to say they are being clicked. Pre-parsing the content before rendering it will help, but it’s a bit of a pain.

If you’re directly rendering the text you’ll probably have to do that pre-parsing anyway and work out a way to make the text clickable. If you’re just using display.newText you’ve got quite a job on your hands.

No, I don’t think webView is a good idea in my case.

Yes, I am trying to show texts with display.newText, I think I am very stuck here.

I saw some apps do what I want to do (showing URL clickable links) without using web views, does it mean they are not using Corona SDK, and XCode can take care of it easily?

If you can get the links as independent chunks where you can display them as their own display.newText() then you have to add a touch or tap handler to the display.newText() showing the text you want clickable and then the function handler for that piece of text would call system.openURL() on the URL for that block of text.

There have been a couple of people on here have built their own text libraries and at least one of them tries to parse out URLs.  They are not libraries that we at Corona have created so I really can’t help you much with them as I’ve never used them personally, but you can search the forums for “text library” or see if it’s in the Community Code somewhere.

Hi Rob

I found some text libraries but I couldn’t find the one that tries to parse out URLs. 

And yes, I was thinking the same to display the URL links as independent chunks and have listeners on them, but it seems much more complicated than I thought. Because the link can be multiple-line and at the same time it can also be surrounded with texts without line breaks. 

For example, a text message like this with an URL inside:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Deinde qui fit, ut ego nesciam, sciant omnes, quicumque Epicurei esse voluerunt? Quae animi affectio suum cuique tribuens atque hanc, quam dico. Si alia sentit, (http://funnestplanetintheworld.com/thearticle?pid=10&key=thefunnythingintheworld&whatelse=alotofthingstobesaidhere) inquam, alia loquitur, numquam intellegam quid sentiat; Id enim volumus, id contendimus, ut officii fructus sit ipsum officium. Duo Reges: constructio interrete. Videamus animi partes, quarum est conspectus illustrior; Polycratem Samium felicem appellabant. Quem si tenueris, non modo meum Ciceronem, sed etiam me ipsum abducas licebit. Callipho ad virtutem nihil adiunxit nisi voluptatem, Diodorus vacuitatem doloris. Aliter enim nosmet ipsos nosse non possumus. Id mihi magnum videtur.

I was originally thinking to have newText() to show the texts until a “http” starts, but then I don’t know the “x” to start next text.

And if I try newText() to print line by line, if the URL is long enough to occupy multiple lines, it becomes very complicated.

Is it possible to know the width of the last line printed by display.newText()?

If I can get the value, it’s possible to do what I intend to do much easier.

.width will have the width of the text if it’s a single line object. If it’s multi-line, then I don’t know of a way as the .width in that case would be the width of the container box.

It depends how you’re displaying the text. If you’re using a web view to show the content the links should work as normal, your app won’t get an event to say they are being clicked. Pre-parsing the content before rendering it will help, but it’s a bit of a pain.

If you’re directly rendering the text you’ll probably have to do that pre-parsing anyway and work out a way to make the text clickable. If you’re just using display.newText you’ve got quite a job on your hands.

No, I don’t think webView is a good idea in my case.

Yes, I am trying to show texts with display.newText, I think I am very stuck here.

I saw some apps do what I want to do (showing URL clickable links) without using web views, does it mean they are not using Corona SDK, and XCode can take care of it easily?

If you can get the links as independent chunks where you can display them as their own display.newText() then you have to add a touch or tap handler to the display.newText() showing the text you want clickable and then the function handler for that piece of text would call system.openURL() on the URL for that block of text.

There have been a couple of people on here have built their own text libraries and at least one of them tries to parse out URLs.  They are not libraries that we at Corona have created so I really can’t help you much with them as I’ve never used them personally, but you can search the forums for “text library” or see if it’s in the Community Code somewhere.

Hi Rob

I found some text libraries but I couldn’t find the one that tries to parse out URLs. 

And yes, I was thinking the same to display the URL links as independent chunks and have listeners on them, but it seems much more complicated than I thought. Because the link can be multiple-line and at the same time it can also be surrounded with texts without line breaks. 

For example, a text message like this with an URL inside:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Deinde qui fit, ut ego nesciam, sciant omnes, quicumque Epicurei esse voluerunt? Quae animi affectio suum cuique tribuens atque hanc, quam dico. Si alia sentit, (http://funnestplanetintheworld.com/thearticle?pid=10&key=thefunnythingintheworld&whatelse=alotofthingstobesaidhere) inquam, alia loquitur, numquam intellegam quid sentiat; Id enim volumus, id contendimus, ut officii fructus sit ipsum officium. Duo Reges: constructio interrete. Videamus animi partes, quarum est conspectus illustrior; Polycratem Samium felicem appellabant. Quem si tenueris, non modo meum Ciceronem, sed etiam me ipsum abducas licebit. Callipho ad virtutem nihil adiunxit nisi voluptatem, Diodorus vacuitatem doloris. Aliter enim nosmet ipsos nosse non possumus. Id mihi magnum videtur.

I was originally thinking to have newText() to show the texts until a “http” starts, but then I don’t know the “x” to start next text.

And if I try newText() to print line by line, if the URL is long enough to occupy multiple lines, it becomes very complicated.

Is it possible to know the width of the last line printed by display.newText()?

If I can get the value, it’s possible to do what I intend to do much easier.

.width will have the width of the text if it’s a single line object. If it’s multi-line, then I don’t know of a way as the .width in that case would be the width of the container box.