iOS 检测文本中的 URL、电话号码等信息

要检测文本中的 URL、电话号码等,除了用正则表达式,还可以用 NSDataDetector。

  1. 用 NSTextCheckingResult.CheckingType 初始化 NSDataDetector
  2. 调用 NSDataDetector 的 matches(in:options:range:) 方法获得 NSTextCheckingResult 数组
  3. 遍历 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、电话号码等信息的更多相关文章

  1. 解决alert在ios版微信中显示url的问题(重写alert)

    为了解决alert在ios版微信中显示url的问题 window.alert = function(name){ var iframe = document.createElement("I ...

  2. PHP实现把文本中的URL转换为链接的auolink()

    转载:http://www.jb51.net/article/52916.htm 其实我在<把文本中的URL地址转换为可点击链接的JavaScript.PHP自定义函数>一文中介绍过PHP ...

  3. PHP正则表达式-从文本中提取URL

    1.从文本中提取URL的正则表达式 '/https?:\/\/[\w-.%#?\/\\\]+/i'

  4. 给文本中的url加超级链接,同时滤过已加过链接的url

    /** * 给文本中的url加超级链接,同时滤过已有链接的url * @param string $str [description] * @return [type] [description] * ...

  5. iOS 设置文本中指定某段文本的颜色 大小

    NSString *money = @"300"; NSString *perStr = @"元/时"; NSString *text = [NSString  ...

  6. iOS:文本视图控件UITextView的详细使用

    文本视图控件:UITextView 介绍:它是一个文本域的编辑视图,可以在该区域上进行编辑(包括删除.剪贴.复制.修改等),它与文本框UITextField的不同之处是:当它里面的每一行内容超出时,可 ...

  7. 图片处理:html文本获取图片Url,判断图片大小,存数据库

    1.从html文本获取图片Url /** * html文本中取出url链接 */ public class Url { public static void main(String[] args) { ...

  8. IOS 正则表达式匹配文本中URL位置并获取URL所在位置(解决连接中文问题)

    需求很简单,是从一段文本中匹配出其中的超链接.基本的做法就是用正则表达式去匹配.但是有这样一个问题. 网上大部分的识别URL的正则表达式url末尾有空格的情况下可以正确识别.比如这样的情况. 我是一段 ...

  9. 在iOS应用程序中使用Frida绕过越狱检测

           阿里聚安全在之前的三篇博客中介绍了利用Frida攻击Android应用程序,整个过程仿佛让开发者开启上帝视角,在本篇博客中,我们将会介绍在iOS应用程序中使用Frida绕过越狱检测.即使 ...

随机推荐

  1. 连连看的原生JS实现

    那天闲来无事,便想找个小游戏来打发时间,后来便找到了连连看, 玩了一会儿感觉无聊,想到各位高手用JS做的各种小游戏,便想自己也来做一个,于是便有了这几天的成果. 代码是用 原生JS 实现的,只是用来学 ...

  2. IDEA下创建Maven项目,并整合使用Spring、Spring MVC、Mybatis框架

    项目创建 本项目使用的是IDEA 2016创建. 首先电脑安装Maven,接着打开IDEA新建一个project,选择Maven,选择图中所选项,下一步. 填写好GroupId和ArtifactId, ...

  3. 杭电OJ2005——第几天

    /*第几天?Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. 【经验】JavaScript

    1.function closeWin(){             window.open('','_self');       window.opener=null;  //    window. ...

  5. 五金配件行业ERP解决方案

    五金行业信息化需求分析 “国内竞争国际化,国际竞争国内化”将是近几年我国五金行业发展的特点,中国作为全球五金制造中心的地位将进一步稳固.随着中国融入全球经济环境进程的加快以及经济实力的快速崛起,中国已 ...

  6. NSTimer定时器进阶——详细介绍,循环引用分析与解决

    引言 定时器:A timer waits until a certain time interval has elapsed and then fires, sending a specified m ...

  7. input解决浏览器记住密码问题

    <input type="password" name="" id="">可以这样写 <input type=" ...

  8. Spring Boot 相关文章目录

    目录 监控 SpringBoot之旅 -- SpringBoot 项目健康检查与监控

  9. 基于 Koa平台Node.js开发的KoaHub.js连接打印机的代码

    最近好多小伙伴都在做微信商城的项目,那就给大家分享一个基于 Koa.js 平台的 Node.js web 开发的框架连接微信易联云打印机接口的代码,供大家学习.koahub-yilianyun 微信易 ...

  10. 2620: [Usaco2012 Mar]Haybale Restacking

    2620: [Usaco2012 Mar]Haybale Restacking Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 201  Solved:  ...