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绕过越狱检测.即使 ...
随机推荐
- WPF移动Window窗体(鼠标点击左键移动窗体自定义行为)
XAML代码部分:1.引用System.Windows.Interactivity 2.为指定的控件添加一个拖动的行为 3.很简单的了解行为的作用和用法 <Window xmlns=" ...
- 点击Robot Framework的桌面快捷图标后,没有反应(没有打开应用程序)
http://www.cnblogs.com/zhengyihan1216/p/6397478.html 这篇文章中介绍了如何安装Robot Framework以及如何在桌面上创建快捷方式. 但是有 ...
- sql server中部分函数功能详解
1.TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. SQL Server 的语法: SELECT TOP number|percen ...
- js获取浏览器宽高
IE中: document.body.clientWidth ==> BODY对象宽度 document.body.clientHeight ==> BODY对象高度 document.d ...
- debian/ubuntu部署java应用小结
近期改的Java应用即将部署,为了强强联合,需要把Java应用部署到linux,我们选择了debian系列.小结一下部署的大致过程,如下: Ubuntu已经默认安装了OpenJDK,但还是比较倾向官方 ...
- KoaHub平台基于Node.js开发的Koa的简单包装到请求库的类似接口
co-request co-request promisify wrapper for request co-request Simple wrapper to the request library ...
- Android实现录屏直播(三)MediaProjection + VirtualDisplay + librtmp + MediaCodec实现视频编码并推流到rtmp服务器
请尊重分享成果,转载请注明出处,本文来自Coder包子哥,原文链接:http://blog.csdn.net/zxccxzzxz/article/details/55230272 Android实现录 ...
- 2761: [JLOI2011]不重复数字(平衡树)
2761: [JLOI2011]不重复数字 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2133 Solved: 825[Submit][Stat ...
- Servlet 3.0/3.1 中的异步处理
在Servlet 3.0之前,Servlet采用Thread-Per-Request的方式处理请求,即每一次Http请求都由某一个线程从头到尾负责处理.如果一个请求需要进行IO操作,比如访问数据库.调 ...
- java 学习资源
1.tomcat版本http://tomcat.apache.org/whichversion.html 2.Servlet 2.5规范https://jcp.org/aboutJava/commun ...