extension NSMutableAttributedString {
func showAttributedStringPhoneNumber() {
let types: UInt64 = NSTextCheckingResult.CheckingType.phoneNumber.rawValue
guard let detector: NSDataDetector = try? NSDataDetector(types: types) else { return }
let matches: [NSTextCheckingResult] = detector.matches(in: self.string, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSRange(location: 0, length: self.length))
for match in matches {
if match.resultType == .phoneNumber, let phoneNumber = match.phoneNumber {
// Get phone number,使用YYText给检测到的手机号添加下划线和点击拨打电话的功能
let str = self.string as NSString
let copIdx = str.range(of: phoneNumber).location
self.yy_setUnderlineStyle(.single, range: NSRange(location: copIdx, length: phoneNumber.count))
self.yy_setUnderlineColor(rgba(190, 154, 98, 1), range: NSRange(location: copIdx, length: phoneNumber.count))
self.yy_setTextHighlight(NSRange(location: copIdx, length: phoneNumber.count), color: rgba(190, 154, 98, 1), backgroundColor: UIColor.clear) { (vew, Str, range, rect) in
UIApplication.shared.openURL(URL(string: "telprompt://\(phoneNumber)")!)
}
}
}
}
}
网友评论