IOS/Objective-C: UITextView not highlighting some phone links

95 views Asked by At

I assemble an array of phone numbers for a Contact and then convert it to a string that, in turn, is inserted in a textView. I then set the textview datadetector type to all.

In the majority of cases, the phone numbers are highlighted but in some cases they are not. The links function so the code is detecting that they are phone links. They just aren't highlighted.

This is not a random occurrence for a given contact and type of number such as office. If a type of number, say office, is not highlighted for a given contact once, it will never be highlighted unless I add or remove other numbers as below.

The phenomenon does appear to be random, however, across phone numbers and contacts. For some contacts and types of numbers, the number is highlighted and for others not

The only other patterns I can discern are 1) if there is more then one number then the earlier numbers beforehand are always highlighted although the last one may or may not be highlighted.

A number is always highlighted if there is another number after it.

If a number is not highlighted (such as office) and I add one after it (such as mobile), the first number will be highlighted and the second one not. If I then remove the second number, the first will once more not be highlighted.

For the life of me, I can't seem to debug this as there is really no reason that I can think of why occasionally the numbers are not highlighted.

Following is the bulk of the code used. Thanks in advance for any suggestions. (I have tried adding spaces at the end of the last number without effect.)

//office  
 NSString *tel = self.contact.tel== nil ? @"" : self.contact.tel;
 NSString *telfor= [self formatPhoneNum: tel];
 telfor = self.contact.tel.length<1  ? @"" :[NSString stringWithFormat: @"o: %@", telfor];
 //mobile
NSString *telmob = self.contact.telmob==nil ? @"" : self.contact.telmob;
NSString *telmobfor= [super formatPhoneNum: telmob];
telmobfor = self.contact.telmob.length<1 ? @"" :[NSString stringWithFormat: @"mob: %@", telmobfor];

    NSMutableArray *telNumsArr = [@[] mutableCopy]; 

    if (telfor.length > 0)
        [telNumsArr addObject:telfor];
    if (telmobfor.length > 0)
        [telNumsArr addObject:telmobfor];

//there is similar code for other types of numbers such as fax NSString *telNums = [telNumsArr componentsJoinedByString:@" "]; NSMutableArray *contBlockArr = [@[] mutableCopy]; // [[NSMutableArray alloc] init];

    if (bestTel.length > 0)
        [contBlockArr addObject:bestTel];

     NSString *contBlock = [contBlockArr componentsJoinedByString:@"\n"];
   self.textView.text = contBlock;
self.textView.dataDetectorTypes = UIDataDetectorTypeAll;




- (NSString*)formatPhoneNum: (NSString*)numberString
{

    NSUInteger length = numberString.length;
    BOOL hasLeadingOne = length > 0 && [numberString characterAtIndex:0] == '1';

    if ((length<7) || length == 0 || (length > 10 && !hasLeadingOne) || (length > 11)) {
        //this is not suitable for reformatting, leave NumberString as is.
        return numberString;
    }

    NSUInteger index = 0;
    NSMutableString *formattedString = [NSMutableString string];

    if (hasLeadingOne) {
        [formattedString appendString:@"1 "];
        index += 1;
    }

    if (length - index > 3) {
        NSString *areaCode = [numberString substringWithRange:NSMakeRange(index, 3)];
        [formattedString appendFormat:@"(%@) ",areaCode];
        index += 3;
    }

    if (length - index > 3) {
        NSString *prefix = [numberString substringWithRange:NSMakeRange(index, 3)];
        [formattedString appendFormat:@"%@-",prefix];
        index += 3;
    }

    NSString *remainder = [numberString substringFromIndex:index];
    [formattedString appendString:remainder];

    return formattedString;

    //    }
}
1

There are 1 answers

0
Thomas Deniau On

You should probably file a bug with Apple. However, Data Detectors is built to detect things in natural language. Building a list programmatically is not a supported use case. If you construct things programmatically and therefore know where your phone numbers are, you can manually create tel: links.