实现Label文本内容中电话号码特殊颜色显示并点击电话号码能拨打
2016-12-05 21:37
375 查看
[objc]
view plain
copy
print?
//获取字符串中的电话号码
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, BOOLBOOL * _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];
}
}];
}
其中_contentStr为_contentLabel要显示的文本内容
效果如下
view plain
copy
print?
//获取字符串中的电话号码
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, BOOLBOOL * _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];
}
}];
}
//获取字符串中的电话号码 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]; 4000 [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]; } }]; }
其中_contentStr为_contentLabel要显示的文本内容
效果如下
相关文章推荐
- 实现Label文本内容中电话号码特殊颜色显示并点击电话号码能拨打
- 用SDK对话框实现改变背景颜色和显示剪贴板中的文本文件内容
- 同一页面中实现点击其中一个iframe中的链接后,将链接内容显示到另一iframe
- JQuery实现鼠标点击文本框显示隐藏提示文本
- shell脚本中echo显示内容带颜色的实现方法
- Delphi实现Listbox中的item根据内容显示不同颜色的方法
- TextView属性android:ellipsize实现跑马灯效果,TextView内容过长加省略号,点击显示全部内容
- 批处理实现文本内容居中显示的代码
- 将两个div左右并列显示并实现点击标题切换内容
- 点击按钮实现内容颜色的改变
- shell脚本中echo显示内容带颜色的实现方法
- ajax实现点击不同的链接让返回的内容显示在特定div里
- JQuery文本点击显示隐藏实现
- Jquery实现点击切换图片并隐藏显示内容
- 如何实现textarea里的不同文本显示不同颜色
- VS2005里自带的日历控件实现点击日期显示当天所更新了内容!
- 基于JQuery实现鼠标点击文本框显示隐藏提示文本
- JS/jQuery实现默认显示部分文字点击按钮显示全部内容
- UITextView显示HTML内容,实现显示不同的字体和文字颜色
- Android实战,实现文本读取,实现assets下资源文本的读取,实现l列表下隐藏TextView点击显示