nsdatadetector

Extracting address elements from a String using NSDataDetector in Swift 3.0

大憨熊 提交于 2021-02-18 06:58:15
问题 I'm attempting to use NSDataDetector to addresses from a string. I've taken a look at NSHipster's article on NSDataDetector as well as Apple's NSDataDetector documentation. I've got the following method to the point where it'll pull addresses out of a string: func getAddress(from dataString: String) -> [String] { let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue) let matches = detector.matches(in: dataString, options: [], range: NSRange(location: 0,

How to detect email addresses within arbitrary strings

本秂侑毒 提交于 2019-12-30 12:51:07
问题 I'm using the following code to detect an email in the string. It works fine except dealing with email having pure number prefix, such as "536264846@gmail.com". Is it possible to overcome this bug of apple? Any help will be appreciated! NSString *string = @"536264846@gmail.com"; NSError *error = NULL; NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error]; NSArray *matches = [detector matchesInString:string options:0 range:NSMakeRange(0, [string

Swift: check if UISearchBar.text contains a url

孤者浪人 提交于 2019-12-20 04:15:06
问题 How can I check if UISearchBar.text contains a URL? I thought of doing something like this: if (searchBar.text == NSTextCheckingType.Link) { } but I get the error: String is not convertible to NSObject 回答1: With Swift 3, you can use NSDataDetector . NSDataDetector has an initializer called init(types:). init(types:) has the following declaration: init(types checkingTypes: NSTextCheckingTypes) throws Initializes and returns a data detector instance. In order to create a data detector that

Detect the “access code” portion of a conference call phone number in an NSString

纵然是瞬间 提交于 2019-12-13 17:20:48
问题 Given an EKEvent that represents a calendar entry on an iOS device, I'm trying to detect not only a phone number but also the related conference call access code. For example, given: NSString *text = @"Blah Blah Blah (555) 123-4567 Access Code: 9876# Blah Blah Blah"; I'd like to implement a method that would return this value: @"555-123-4567,,9876#" Calendar entries might use various text besides @"Access Code: " to describe the number used to access a conference call. For example, other

How to disable magnification in UITextView

随声附和 提交于 2019-12-10 21:58:12
问题 I want to get rid of magnification and text selection in UITextView but I need phone number, link and address detectors. I am using -(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer { if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) { gestureRecognizer.enabled = NO; } [super addGestureRecognizer:gestureRecognizer]; return;} to stop magnification, but it also stops selection phone number / address / link detected by textview. If I do [_txtView

NSDate detection with NSDataDetector

北慕城南 提交于 2019-12-03 09:41:08
问题 I tried to get a NSDate from a NSString with UNKNOWN format, So I wrote a function like below -(void)dateFromString:(NSString*)string { NSError *error = NULL; NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error]; NSArray *matches = [detector matchesInString:string options:0 range:NSMakeRange(0, [string length])]; NSLocale* currentLoc = [NSLocale currentLocale]; for (NSTextCheckingResult *match in matches) { if ([match

NSDataDetector detecting “phone number” text

给你一囗甜甜゛ 提交于 2019-11-30 23:55:56
问题 The easiest way I can explain this problem is with a code sample and its output, but essentially what's happening is NSDataDetector is detecting a phone number within a string that includes the words "phone number". NSError *error = nil; NSDataDetector *dataDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error]; NSArray *stringsToTest = @[ @"testing phone number 0123 4567891", @"testing some other number 0123 4567892", @"phone number 0123 4567893", @"blah