iOS檢測(cè)文本中的URL、電話號(hào)碼等信息

要檢測(cè)文本中的 URL、電話號(hào)碼等,除了用正則表達(dá)式,還可以用 NSDataDetector。

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括海淀網(wǎng)站建設(shè)、海淀網(wǎng)站制作、海淀網(wǎng)頁制作以及海淀網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,海淀網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到海淀省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

  1. 用 NSTextCheckingResult.CheckingType 初始化 NSDataDetector
  2. 調(diào)用 NSDataDetector 的 matches(in:options:range:) 方法獲得 NSTextCheckingResult 數(shù)組
  3. 遍歷 NSTextCheckingResult 數(shù)組,根據(jù)類型獲取相應(yīng)的檢測(cè)結(jié)果,通過 range 獲取結(jié)果文本在原文本中的位置范圍(NSRange)

下面的例子是把 NSMutableAttributedString 中的 URL、電話號(hào)碼突出顯示。

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,實(shí)際上是 UInt64??梢杂没蜻\(yùn)算符連接多個(gè)值,以實(shí)現(xiàn)同時(shí)檢測(cè)多種類型的文本。

public typealias NSTextCheckingTypes = UInt64

NSTextCheckingResult 的檢測(cè)結(jié)果屬性與類型有關(guān)。例如,當(dāng)檢測(cè)類型是 URL (resultType == .link),就可以通過 url 屬性獲取檢測(cè)到的 URL。

給 NSMutableAttributedString 添加下劃線,NSUnderlineStyleAttributeName 作為 key 對(duì)應(yīng)的值在 Swift 中可以為 Int,不能為 NSUnderlineStyle。所以要寫NSUnderlineStyle.styleSingle.rawValue。寫NSUnderlineStyle.styleSingle會(huì)導(dǎo)致 NSMutableAttributedString 顯示不出來。

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持創(chuàng)新互聯(lián)!

本文標(biāo)題:iOS檢測(cè)文本中的URL、電話號(hào)碼等信息
標(biāo)題鏈接:http://bm7419.com/article44/gipjhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、搜索引擎優(yōu)化定制網(wǎng)站定制開發(fā)、面包屑導(dǎo)航、微信公眾號(hào)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司