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

来源:互联网 发布:测帧数软件 编辑:程序博客网 时间:2024/04/29 18:34
#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]];                    }}

0 1
原创粉丝点击