美文网首页
iOS开发,识别文字中的手机号高亮显示并可以拨打

iOS开发,识别文字中的手机号高亮显示并可以拨打

作者: 钟南山人 | 来源:发表于2019-02-22 16:11 被阅读0次

#pragma mark - <识手机号>

- (void)validPhoneNumFromString:(NSString*)string {

    NSError*error;

    //根据检测的类型初始化

    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error];

    //获得检测所得到的数组

    NSRange stringRange =NSMakeRange(0, string.length);

    NSArray *matches = [detector matchesInString:string options:0 range:stringRange];

    for(NSTextCheckingResult *match in matches) {

        if (match.resultType == NSTextCheckingTypePhoneNumber) {

            NSRange phoneRange = [match range];

            _phoneStr = [string substringWithRange:phoneRange];

            if(_phoneStr.length>0) {

                _bubbleView.textLabel.userInteractionEnabled = YES;

                UITapGestureRecognizer*tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(phoneLink)];

                [_bubbleView.textLabeladdGestureRecognizer:tap];

            }

        }

    }

}

#pragma mark - 点击拨打电话

- (void)phoneLink {

    NSMutableString *phone = [[NSMutableString alloc] initWithFormat:@"tel:%@",_phoneStr];

     [[UIApplication sharedApplication] openURL:    [NSURL URLWithString:phone]];

}

相关文章

网友评论

      本文标题:iOS开发,识别文字中的手机号高亮显示并可以拨打

      本文链接:https://www.haomeiwen.com/subject/usmeyqtx.html