Author Topic: Make URL as clickable in Lable string  (Read 4946 times)

swaraj

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Make URL as clickable in Lable string
« on: February 06, 2014, 05:46:35 AM »
Hi all,

How can we achieve this? I've a string with urls lets say "something http://www.google.com again http://www.amazon.com" or "http://www.google.com nexturl http://www.amazon.com". Now what I want is, when I am showing this string (in a UILabel), I could make the link clickable, i.e., the link should act like a url, so that when I click it, it should open me the respective link.

This topic relates the same I wanted to achieve : http://www.tasharen.com/forum/index.php?topic=7809.0, but it does not provide any solutions :(

Please provide me some suggestions on how to achieve this.

Thanks in advance.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Make URL as clickable in Lable string
« Reply #1 on: February 06, 2014, 09:39:38 PM »
There is no such functionality in NGUI. Not yet. It's on the list of things to add.

You can currently hack it just by using label.GetCharacterIndex(UICamera.lastHit.point) to determine the index of the character under the mouse, then see if you're over your link by doing some text parsing.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Make URL as clickable in Lable string
« Reply #2 on: February 06, 2014, 09:51:03 PM »
Here's the full script:
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(UILabel))]
  4. public class ClickableLink : MonoBehaviour
  5. {
  6.         void OnClick ()
  7.         {
  8.                 UILabel lbl = GetComponent<UILabel>();
  9.  
  10.                 int index = lbl.GetCharacterIndex(UICamera.lastHit.point);
  11.                
  12.                 if (index != -1)
  13.                 {
  14.                         string text = lbl.text;
  15.  
  16.                         int linkStart = text.LastIndexOf(' ', index, index) + 1;
  17.                         int linkEnd = text.IndexOf(' ', index);
  18.                         if (linkEnd == -1) linkEnd = text.Length;
  19.                         if (linkStart == linkEnd) return;
  20.  
  21.                         string url = text.Substring(linkStart, linkEnd - linkStart);
  22.                         if (url.StartsWith("http://")) Application.OpenURL(url);
  23.                 }
  24.         }
  25. }