iOS UITextView Link detection

5.3k Views Asked by At

I have a UITextView with Detection Links selected and behaviour selectable and editable are unchecked. It detects links with prefix 'www or http:// or https:// etc` but it simply not get detects goole.co.in. Any solution.

2

There are 2 best solutions below

1
HardikDG On

This may not be the solution for your question but i have found one weird issue based on your question

For ex :

    txtTest.userInteractionEnabled = true;
    txtTest.selectable = true
    txtTest.dataDetectorTypes = .Link;
    txtTest.text = "stackoverflow.com"

When i write this code it detect the link in the textview , the same case will be there for the 'google.co.uk','google.co.us','google.com' if it write any of them in the text it detects the link

But as you mention in the question if we change the text to 'google.co.in' it don't detect the link , i have tried with 'google.co.test' this also don't detect the link

So it may be something related to default link detection pattern of the 'dataDetectorTypes', else your code may work for other url's

If you have static link links you can check if this links are detected in the textview

Else as another option you may try UIWebView if it works in your case

1
Johnny Rockex On

Alternatively, run your own checks if Apple's aren't pulling up what you need. Run through the text output from UITextView and look for specific keywords, such as '.co.in', and should they exist, highlight them (or whatever).

NSString * text = textView.text;
if ([text containsString:".co.in"]){
   //Indian domain found
}

UPDATE

You can highlight specific text in a string using attributedStrings:

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:myString];
UIFont * boldFont = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0f];
NSDictionary * attributedDict = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, [UIColor blueColor], NSForegroundColorAttributeName, nil];
NSRange range = [myString rangeOfString:@"www.google.co.in"];
[attributedString setAttributes:attributedDict range:range];
[myLabel setAttributedText:attributedString];

Regarding touches, it would be possible to place a tap gesture recogniser over the entire UILabel and then detect the location of the tap using this:

-(void)longPressDetected:(UILongPressGestureRecognizer *)sender {
    CGPoint touchLocation = [sender locationInView:myLabel];
}

and from there check if the tap location overlaps with the known frames of words.

I get the feeling there might be an easier way to achieve what you want to do, but I can't remember what it is.