美文网首页iOS小功能点iOS Developerios 知识点
iOS 识别文字中的手机号码高亮显示点击可拨打电话

iOS 识别文字中的手机号码高亮显示点击可拨打电话

作者: 陨之希留leo | 来源:发表于2017-03-28 17:39 被阅读1988次

    前一阵子项目遇到一个需求:标签展现的文字中需要识别手机号码,高亮显示添加下划线并可以点击拨打电话。



    直接上代码,写了一个方法,传入需要进行判断的标签及其标签的文字内容

    -(void)distinguishPhoneNumLabel:(UILabel *)label labelStr:(NSString *)labelStr{
    
    //获取字符串中的电话号码
    NSString *regulaStr = @"\\d{3,4}[- ]?\\d{7,8}";
    NSRange stringRange = NSMakeRange(0, labelStr.length);
    //正则匹配
    NSError *error;
    
     NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:labelStr];
    
    NSRegularExpression *regexps = [NSRegularExpression regularExpressionWithPattern:regulaStr options:0 error:&error];
    if (!error && regexps != nil) {
        [regexps enumerateMatchesInString:labelStr options:0 range:stringRange usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
            
            NSRange phoneRange = result.range;
            //定义一个NSAttributedstring接受电话号码字符串
            phoneNumber = [str attributedSubstringFromRange:phoneRange];
            //添加下划线
            NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            [str addAttributes:attribtDic range:phoneRange];
            //设置文本中的电话号码显示为黄色
            [str addAttribute:NSForegroundColorAttributeName value:[LFWImage colorWithHexString:@"FF8200"] range:phoneRange];
            
            label.attributedText = str;
            label.userInteractionEnabled = YES;
            
            //添加手势,可以点击号码拨打电话
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        
            [label addGestureRecognizer:tap];
        
        }];
    }
    
    }
    

     //实现拨打电话的方法
    -(void)tapGesture:(UITapGestureRecognizer *)sender{
    
     NSString *deviceType = [UIDevice currentDevice].model;
    if([deviceType  isEqualToString:@"iPod touch"]||[deviceType  isEqualToString:@"iPad"]||[deviceType  isEqualToString:@"iPhone Simulator"]){
        
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"您的设备不能打电话" delegate:nil cancelButtonTitle:@"好的,知道了" otherButtonTitles:nil,nil];
        
        [alert show];
        
    }else{
      
        //NSAttributedstring转换为NSString
        NSString *stringNum = [phoneNumber string];
        NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",stringNum];
        NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        UIWebView * callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newStr]]];
        [self.view addSubview:callWebview];
    
    }
    
    }

    相关文章

      网友评论

      • iOS_渔翁:想问问 这个正则是什么意思
        感觉不太全啊
      • andy桐:你这个是点击label触发的,只点击号码怎么整,用NSLinkAttributeName么,可是用NSLinkAttributeName点击触发的效果又没有那么理想(控件用的UITextView),感觉总是不好点,有什么好的方法么(我这边需求是在物流信息中点击电话)

      本文标题:iOS 识别文字中的手机号码高亮显示点击可拨打电话

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