要檢測文本中的 URL、電話號碼等,除了用正則表達式,還可以用 NSDataDetector。
下面的例子是把 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) } } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞名稱:iOS檢測文本中的URL、電話號碼等信息-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://chinadenli.net/article40/dggheo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、虛擬主機、網(wǎng)站排名、App開發(fā)、企業(yè)建站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容