How do you put links to websites in labels?
I'm curious whether there is a way to hyperlink to a website a la html anchor tags style. I have a label and may either want to linkify the entire label or a part of it.
I'm hoping that I don't need to create a "mock" link with an underline styling with an event listener that opens the webview and sends the user accordingly.
Any thoughts?
5 Answers
-
There is a very simple and cross-platform solution here: http://developer.appcelerator.com/question/11281/click-button-and-open-hyperlink-in-safari-instead-of-webview#comment-108572
The code is:
//create label (or button etc) var label = Ti.UI.createLabel({text:'LINK'}); //event listener label.addEventListener('click', function(e) { //open link in safari - application will close Titanium.Platform.openURL('http://www.soft4u.be'); });
-
Hi,
I know it's an old topic, but here is my solution:
var link = "http://developer.appcelerator.com/apidoc/mobile/latest/Titanium.UI.Label-object"; var linkE = Titanium.UI.createLabel({ text: "What about label in titanium?", color: "blue", font: { fontSize: 16, fontWeight: 'bold' }, width: 'auto', textAlign: 'left', bottom: deviceHeight * 2 / 100, left: deviceWidth * 4/100, height: 'auto', }); linkE.addEventListener('click', function(e){ var intent = Ti.Android.createIntent({ action: Ti.Android.ACTION_VIEW, data:link }); Ti.Android.currentActivity.startActivity(intent); }); win.add(linkE);
in fact it works for almost everything (intent can be use to start google map/e-mail/dial/…)
-
Unfortunately Surojit, that is precisely what is needed right now :(. We've had other people run into this exact problem, and that is the technique needed - you'll need to specially style labels and add a click handler function to launch the browser.
-
This is a method I used….may need some tweeking:
var createLink = function(labelOptions, callback) { var view = Titanium.UI.createView({ width:'auto', height:'auto', layout:'vertical' }); var label = Titanium.UI.createLabel(labelOptions); label.addEventListener('click', callback); var lineView = Titanium.UI.createView({ width:label.width, left:label.left, height:1, backgroundColor:label.color ? label.color : 'black', bottom:0 }); view.add(label); view.add(lineView); return view; } var forgotPassword = createLink({ width:'auto', height:20, text:'Forgot Password?', left:120 }, function(){Titanium.Platform.openURL('http://someurl.com/forgotpassword')});
-
for android …
From below code set html property of label see in double quotevar lblLink = Titanium.UI.createLabel({
font : {
fontSize : 20,
fontStyle : 'bold',
},
top : 10,
color : '#0000EE',
left : 10,
height : 30,
width : 150,
html : refer this
});