实现Label文本内容中电话号码特殊颜色显示并点击电话号码能拨打

#pramark-<识手机号>
- (void)validPhoneNum{ 
       //获取字符串中的电话号码
        NSString *regulaStr = @"\\d{3,4}[- ]?\\d{7,8}";
        NSRange stringRange = NSMakeRange(0, _contentStr.length);
        //正则匹配
        NSError *error;
        NSRegularExpression *regexps = [NSRegularExpression regularExpressionWithPattern:regulaStr options:0 error:&error];
        if (!error && regexps != nil) {
            [regexps enumerateMatchesInString:_contentStr options:0 range:stringRange usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {

                //可能为电话号码的字符串及其所在位置
                //           NSString *actionString = [NSString stringWithFormat:@"%@",[_contentStr substringWithRange:result.range]];
                NSRange phoneRange = result.range;

                //            NSLog(@"%@-----%@", actionString, NSStringFromRange(phoneRange));

                //设置文本中的电话号码显示为蓝色
                NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:_contentStr];
                [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:phoneRange];
                _contenLabel.attributedText = str;

                //点击拨打电话
                UIControl *phoneControl = [_contenLabel viewWithTag:1234];
                if (phoneControl == nil) {
                    UIControl *phoneControl = [[UIControl alloc] initWithFrame:[self boundingRectForCharacterRange:phoneRange]];
                    phoneControl.tag = 1234;
                    [phoneControl addTarget:self action:@selector(phoneLink) forControlEvents:UIControlEventTouchUpInside];
                    [_contenLabel addSubview:phoneControl];
                }          
            }];
        }
}

#pragma mark-<获取电话号码的坐标>
- (CGRect)boundingRectForCharacterRange:(NSRange)range andContentStr:(NSString *)contentStr
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:contentStr];
    NSDictionary *attrs =@{NSFontAttributeName : [UIFont systemFontOfSize:14.0]};
    [attributeString setAttributes:attrs range:NSMakeRange(0, contentStr.length)];

    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:attributeString];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:[self bounds].size];
    textContainer.lineFragmentPadding = 0;
    [layoutManager addTextContainer:textContainer];

    NSRange glyphRange;

    [layoutManager characterRangeForGlyphRange:range actualGlyphRange:&glyphRange];

    CGRect rect = [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
    CGFloat yOfset =  rect.origin.y;
    rect.origin.y = yOfset + 4;

    return rect;
}
#pragma mark-点击拨打电话
- (void)phoneLink{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"您确定要拨打电话吗?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];

}

#pragma mark-<UIAlertViewDelegate>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //取消

    }else{
        //确定
        NSMutableString *phone = [[NSMutableString alloc] initWithFormat:@"tel:%@",_phoneStr];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phone]];
        
        
    }
}

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值