实现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
- 实现Label文本内容中电话号码特殊颜色显示并点击电话号码能拨打
- 实现Label文本内容中电话号码特殊颜色显示并点击电话号码能拨打
- ios lable中电话号码点击拨打电话
- android 拨打特殊字符电话号码
- 关于在textvie中提取电话号码,点击跳转拨打界面
- android 拨打电话号码
- 手机访问页面电话号码,点击弹出拨打电话
- Contacts---android中实现调用通信录并返回电话号码
- PHP点击电话号码图片 跳到打电话、、、、通过奇偶定义每一行显示的颜色
- 实现TextView中的短电话号码可点击
- 点击网页中的电话号码,实现“呼叫功能”
- js 拨打电话号码的方法
- web移动端拨打电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- HP 畅游人系列系统恢复
- spring mvc junit 上传文件
- 支持Windows和Linux的文件目录创建和删除操作
- 在 idea 中使用git 上传项目到 开源中国
- postgres数据库导入导出数据
- 实现Label文本内容中电话号码特殊颜色显示并点击电话号码能拨打
- shape画圆点
- 说说web缓存
- Android开发环境搭建
- 闭包的生命周期
- iOS开发之支付宝集成
- MWPhotoBrowser的基本使用及图片尺寸的更改
- AP收款导入相关步骤
- Android各大热补丁方案分析和比较