user3470987 user3470987 - 2 months ago 21
iOS Question

Set multiple links in UITextView within a UITableViewCell

I have a tableview that will display multiple addresses as well as some other info in the same cell. After lots of tinkering and googling I came to the conclusion that

UITextView
is the best/easiest way to detect links.

My current approach with
UITextView
, however, is not detecting links and I am not sure why.

Here it is:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
UITableViewCell cell = tableView.DequeueReusableCell(Identifier);
if (cell == null)
cell = new UITableViewCell(UITableViewCellStyle.Value1, Identifier);

cell.TextLabel.Lines = 0; // allow wrapping
cell.TextLabel.Text = items[indexPath.Row];
UITextView tv = new UITextView(new CoreGraphics.CGRect(cell.Bounds.X, cell.Bounds.Y, cell.ContentView.Frame.Size.Width, cell.ContentView.Frame.Size.Height));
tv.Text = cell.TextLabel.Text;
cell.TextLabel.Text = "";
tv.DataDetectorTypes = UIDataDetectorType.Address;
tv.Selectable = true;
tv.UserInteractionEnabled = true;
tv.ScrollEnabled = false;

cell.ContentView.AddSubview(tv);
return cell;
}


This neither detects the embedded address nor allows for it to be clicked.

Been working on this for too long now, took about 3 minutes in Android! Why is this so difficult??

Answer

Replace below line

  tv.DataDetectorTypes = UIDataDetectorType.Address;

with

 tv.DataDetectorTypes = UIDataDetectorType.Link;

And for detecting link in UITextView, your UITextView should be selectable. so make sure that your textview is selectable and not editable like,

  tv.editable = false;
Comments