iOS 检测文本中的 URL、电话号码等信息
iOS 检测文本中的 URL、电话号码等信息
要检测文本中的 URL、电话号码等,除了用正则表达式,还可以用 NSDataDetector。
- 用 NSTextCheckingResult.CheckingType 初始化 NSDataDetector
- 调用 NSDataDetector 的 matches(in:options:range:) 方法获得 NSTextCheckingResult 数组
- 遍历 NSTextCheckingResult 数组,根据类型获取相应的检测结果,通过 range 获取结果文本在原文本中的位置范围(NSRange)
下面的例子是把 NSMutableAttributedString 中的 URL、电话号码突出显示。
func showAttributedStringLink(_ attributedStr: NSMutableAttributedString) {
// We check URL and phone number
let types: UInt64 = NSTextCheckingResult.CheckingType.link.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue
// Get NSDataDetector
guard let detector: NSDataDetector = try? NSDataDetector(types: types) else { return }
// Get NSTextCheckingResult array
let matches: [NSTextCheckingResult] = detector.matches(in: attributedStr.string, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSRange(location: 0, length: attributedStr.length))
// Go through and check result
for match in matches {
if match.resultType == .link, let url = match.url {
// Get URL
attributedStr.addAttributes([ NSLinkAttributeName : url,
NSForegroundColorAttributeName : UIColor.blue,
NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue ],
range: match.range)
} else if match.resultType == .phoneNumber, let phoneNumber = match.phoneNumber {
// Get phone number
attributedStr.addAttributes([ NSLinkAttributeName : phoneNumber,
NSForegroundColorAttributeName : UIColor.blue,
NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue ],
range: match.range)
}
}
}
用于初始化 NSDataDetector 的参数 types 的类型是 NSTextCheckingTypes,实际上是 UInt64。可以用或运算符连接多个值,以实现同时检测多种类型的文本。
public typealias NSTextCheckingTypes = UInt64
NSTextCheckingResult 的检测结果属性与类型有关。例如,当检测类型是 URL (resultType == .link),就可以通过 url 属性获取检测到的 URL。
给 NSMutableAttributedString 添加下划线,NSUnderlineStyleAttributeName 作为 key 对应的值在 Swift 中可以为 Int,不能为 NSUnderlineStyle。所以要写NSUnderlineStyle.styleSingle.rawValue。写NSUnderlineStyle.styleSingle会导致 NSMutableAttributedString 显示不出来。
转载请注明出处:http://www.cnblogs.com/silence-cnblogs/p/6682421.html
iOS 检测文本中的 URL、电话号码等信息的更多相关文章
- 解决alert在ios版微信中显示url的问题(重写alert)
为了解决alert在ios版微信中显示url的问题 window.alert = function(name){ var iframe = document.createElement("I ...
- PHP实现把文本中的URL转换为链接的auolink()
转载:http://www.jb51.net/article/52916.htm 其实我在<把文本中的URL地址转换为可点击链接的JavaScript.PHP自定义函数>一文中介绍过PHP ...
- PHP正则表达式-从文本中提取URL
1.从文本中提取URL的正则表达式 '/https?:\/\/[\w-.%#?\/\\\]+/i'
- 给文本中的url加超级链接,同时滤过已加过链接的url
/** * 给文本中的url加超级链接,同时滤过已有链接的url * @param string $str [description] * @return [type] [description] * ...
- iOS 设置文本中指定某段文本的颜色 大小
NSString *money = @"300"; NSString *perStr = @"元/时"; NSString *text = [NSString ...
- iOS:文本视图控件UITextView的详细使用
文本视图控件:UITextView 介绍:它是一个文本域的编辑视图,可以在该区域上进行编辑(包括删除.剪贴.复制.修改等),它与文本框UITextField的不同之处是:当它里面的每一行内容超出时,可 ...
- 图片处理:html文本获取图片Url,判断图片大小,存数据库
1.从html文本获取图片Url /** * html文本中取出url链接 */ public class Url { public static void main(String[] args) { ...
- IOS 正则表达式匹配文本中URL位置并获取URL所在位置(解决连接中文问题)
需求很简单,是从一段文本中匹配出其中的超链接.基本的做法就是用正则表达式去匹配.但是有这样一个问题. 网上大部分的识别URL的正则表达式url末尾有空格的情况下可以正确识别.比如这样的情况. 我是一段 ...
- 在iOS应用程序中使用Frida绕过越狱检测
阿里聚安全在之前的三篇博客中介绍了利用Frida攻击Android应用程序,整个过程仿佛让开发者开启上帝视角,在本篇博客中,我们将会介绍在iOS应用程序中使用Frida绕过越狱检测.即使 ...
随机推荐
- Xcode的中常用到的快捷键,印象笔记中常用到的快捷键
Xcode提供了很多快捷键,灵活使用快捷键可以提升开发效率.但对于初学者来说,一次性的去记住并掌握如此多的快捷键显然是不现实的,本文就是来帮助大家了解在iOS开发过程中,使用最频繁的一些快捷键. 1. ...
- Ubuntu 小白安装血泪史
介绍: 新入手的Ubuntu:版本 命令行模式下 出现 ♦♦♦♦ 图形界面无法获取最高权限:gurb.cfg 无法再图形界面下修改 安装类型: 1.安装Ubuntu,与Window 7共存 2.清除整 ...
- Linux的capability深入分析
Linux的capability深入分析详见:http://blog.csdn.net/u014338577/article/details/48791953 lxd中对容器能力的限制: 普通用户不能 ...
- Mac OSX Sierra WiFi connecting problem
吐槽一下,苹果的质量管控越来越差了. Mac OSX Sierra有时突然或升级后会遇到wifi不停重连连不上问题,现象为不停地连接wifi. 网上有人说删除 /Library/Preferences ...
- ASP日期格式化函数
Public Function GetFormatDate(DateAndTime, para)On Error Resume NextDim y, m, d, h, mi, s, strDateTi ...
- KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架之koahub-skip
koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is m ...
- 算法模板——splay区间反转 1
实现的功能:将序列区间反转,并维护 详见BZOJ3223 var i,j,k,l,m,n,head,a1,a2:longint; s1:ansistring; a,b,c,d,fat,lef,rig: ...
- 原生js更改css样式的两种方式
下面我给大家介绍的是原生js更改CSS样式的两种方式: 1通过在javascript代码中的node.style.cssText="css表达式1:css表达式2:css表达式3 &quo ...
- install g++ on windows
install c++/g++ on windows install c++/g++ on windows link 原文 1. 算是提示吧: Pick the drive and a fol ...
- react router路径的匹配原则
路由匹配规则是从上到下执行,一旦发现匹配,就不再其余的规则了. (1):paramName :paramName匹配URL的一个部分,直到遇到下一个/.?.#为止.这个路径参数可以通过this.pro ...