iOS 檢測文本中的URL、電話號碼等信息
要檢測文本中的 URL、電話號碼等,除了用正則表達式,還可以用 NSDataDetector。
- 用 NSTextCheckingResult.CheckingType 初始化 NSDataDetector
- 調(diào)用 NSDataDetector 的 matches(in:options:range:) 方法獲得 NSTextCheckingResult 數(shù)組
- 遍歷 NSTextCheckingResult 數(shù)組,根據(jù)類型獲取相應(yīng)的檢測結(jié)果,通過 range 獲取結(jié)果文本在原文本中的位置范圍(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 的參數(shù) types 的類型是 NSTextCheckingTypes,實際上是 UInt64。可以用或運算符連接多個值,以實現(xiàn)同時檢測多種類型的文本。
public typealias NSTextCheckingTypes = UInt64
NSTextCheckingResult 的檢測結(jié)果屬性與類型有關(guān)。例如,當(dāng)檢測類型是 URL (resultType == .link),就可以通過 url 屬性獲取檢測到的 URL。
給 NSMutableAttributedString 添加下劃線,NSUnderlineStyleAttributeName 作為 key 對應(yīng)的值在 Swift 中可以為 Int,不能為 NSUnderlineStyle。所以要寫NSUnderlineStyle.styleSingle.rawValue
。寫NSUnderlineStyle.styleSingle
會導(dǎo)致 NSMutableAttributedString 顯示不出來。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
- iOS 10撥打系統(tǒng)電話彈出框延遲出現(xiàn)問題的解決
- iOS 撥打電話代碼的三種方式
- IOS如何替換電話號碼中間4位為"-"符號
- IOS程序開發(fā)之跳轉(zhuǎn)短信發(fā)送界面實現(xiàn)發(fā)送短信功能
- IOS中快速集成短信SDK驗證開發(fā)(SMSSDK),IOS開發(fā)中如何設(shè)置手機短信驗證碼
- 兩種iOS調(diào)用系統(tǒng)發(fā)短信的方法
- iOS開發(fā)中實現(xiàn)郵件和短信發(fā)送的簡單示例
- 用Swift構(gòu)建一個簡單的iOS郵件應(yīng)用的方法
- iOS打電話、發(fā)短信、發(fā)郵件實例代碼
相關(guān)文章
IOS 關(guān)鍵字const 、static、extern詳解
這篇文章主要介紹了IOS 關(guān)鍵字const 、static、extern詳解的相關(guān)資料,這里對關(guān)鍵字如何使用,及在IOS開發(fā)中的意義做了詳解,需要的朋友可以參考下2016-11-11iOS開發(fā)之?dāng)r截URL轉(zhuǎn)換成本地路由模塊URLRewrite詳解
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)之?dāng)r截URL轉(zhuǎn)換成本地路由模塊URLRewrite的相關(guān)資料,這是最近在工作中遇到的一個需求,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起看看吧。2017-08-08IOS開發(fā)代碼分享之設(shè)置UISearchBar的背景顏色
在項目開發(fā)中,我們經(jīng)常要用到UISearchBar,在網(wǎng)上看到了很多關(guān)于去除掉他背景色的方法,都已經(jīng)失效了,今天來分享一個正常使用的方法,希望能幫到大家2014-09-09ios 流媒體播放器實現(xiàn)流程及FreeStreamer的使用的示例
本篇文章主要介紹了ios 流媒體播放器實現(xiàn)流程及FreeStreamer的使用的示例代碼,非常具有實用價值,需要的朋友可以參考下2018-01-01