Only first link is detected in uitextview

2019-09-10 08:33发布

问题:

I have strange problem with url detection in textView. My code is like this:

_contactDescription.dataDetectorTypes = UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
[_contactDescription setUserInteractionEnabled:YES];
[_contactDescription setSelectable:YES];
[_contactDescription setEditable:NO];
_contactDescription.delegate = self;
_contactDescription.text = desc;

My string has a lot of phone numbers and emails to detect but using data detection only first phone and first email run action on tap. All phones and emails are highlighted but no action on tap.

Does anyone had similar problem ? Thanks in advance.

回答1:

If they are highlighted but you can't click them, but you can click the first one, that usually means that there is another view you didn't expect over lapping the text. You could try adding this in your view controller to see what is getting tapped.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch * touch in [touches allObjects])
    {
        NSLog(@"View: %@", touch.view);
    }
}

Or run it in the simulator and in Debug turn on Color Blended Layers

Or try hiding all other views until it works to find the offender.

Another issue could bet the text is actually outside the view and you can't tell because it isn't clipping the extra. To verify that isn't the case.

[_contactDescription setClipsToBounds:YES];

Hopefully that is the case and one of those help find the offender. Good luck.