美文网首页
UITextView 识别链接、电话、地址

UITextView 识别链接、电话、地址

作者: 梦里桃花舞倾城 | 来源:发表于2018-06-05 15:05 被阅读30次

    UITextView 识别链接、电话、地址

    识别一段文字中是否包含链接/电话/地址我们只需要设置UITextView这个属性就OK了

    self.textView.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
    
    
    self.textView.editable = NO;编辑模式设为NO,才可以开启识别模式

    type枚举类型

    typedef NS_OPTIONS(NSUInteger, UIDataDetectorTypes) {
        UIDataDetectorTypePhoneNumber                                        = 1 << 0, // Phone number detection
        UIDataDetectorTypeLink                                               = 1 << 1, // URL detection
        UIDataDetectorTypeAddress NS_ENUM_AVAILABLE_IOS(4_0)                 = 1 << 2, // Street address detection
        UIDataDetectorTypeCalendarEvent NS_ENUM_AVAILABLE_IOS(4_0)           = 1 << 3, // Event detection
        UIDataDetectorTypeShipmentTrackingNumber NS_ENUM_AVAILABLE_IOS(10_0) = 1 << 4, // Shipment tracking number detection
        UIDataDetectorTypeFlightNumber NS_ENUM_AVAILABLE_IOS(10_0)           = 1 << 5, // Flight number detection
        UIDataDetectorTypeLookupSuggestion NS_ENUM_AVAILABLE_IOS(10_0)       = 1 << 6, // Information users may want to look up
        UIDataDetectorTypeNone          = 0,               // Disable detection
        UIDataDetectorTypeAll           = NSUIntegerMax    // Enable all types, including types that may be added later
    } __TVOS_PROHIBITED;
    
    如果我们想判断一个stirng里面是否包含 电话号码 URL 地址 日期 还在为写一正则而苦恼😞?

    NSDataDetector 可以轻松帮我们搞定

    使用:

    NSString * string = @"欢迎访问http://www.111cn.net,https://111cn.net\n以及ftp://111cn.net";
    NSError * error = nil;
    NSDataDetector * detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber| NSTextCheckingTypeLink  error:&error];
    NSUInteger numberOfMatches = [detector numberOfMatchesInString:string                                                  options:0                                                         range:NSMakeRange(0, [string length])];
    
    

    matches(in:options:range:)的用法:
    查看一共有多少匹配项.还有matches(in:options:range:)和firstMatch(in:options:range:)

    NSString * string = @"欢迎访问http://www.jianshu.com/users/72ee5da886ff/latest_articles. 咱的电话是012-1304445928.ps:电话随便写的哟.今天是2016-10-25,天气(weather)不错";
        
    NSString * string = @"欢迎访问http://www.jianshu.com/users/72ee5da886ff/latest_articles. 咱的电话是012-1304445928.ps:电话随便写的哟.今天是2016-10-25,天气(weather)不错";
    NSError * error = nil;
    NSDataDetector * detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber error:&error];
        
    NSArray *matches = [detector matchesInString:string
                                             options:0
                                               range:NSMakeRange(0, [string length])];
    for (NSTextCheckingResult *match in matches) {
            NSRange matchRange = [match range];
            if ([match resultType] == NSTextCheckingTypeLink) {
                NSURL *url = [match URL];
                NSLog(@"url:%@", url);
            } else if ([match resultType] == NSTextCheckingTypePhoneNumber) {
                NSString *phoneNumber = [match phoneNumber];
                NSLog(@"phoneNumber:%@", phoneNumber);
            }
    }
    
    

    块用法

    NSString * string = @"欢迎访问http://www.jianshu.com/users/72ee5da886ff/latest_articles. 咱的电话是012-1304445928.ps:电话随便写的哟.今天是2016-10-25,天气(weather)不错";
        NSError * error = nil;
        NSDataDetector * detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber error:&error];
        
        
        __block NSUInteger count = 0;
        [detector enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult * _Nullable match, NSMatchingFlags flags, BOOL * _Nonnull stop) {
            
            NSLog(@"flag:%lu",(unsigned long)flags);
            NSRange matchRange = [match range];
            
            if ([match resultType] == NSTextCheckingTypeLink) {
                NSURL *url = [match URL];
                NSLog(@"url:%@", url);
            }
            if (count == 0) *stop = YES;
            
            if ([match resultType] == NSTextCheckingTypePhoneNumber) {
                NSString *phoneNumber = [match phoneNumber];
                NSLog(@"phoneNumber:%@", phoneNumber);
            }
            
        }];
    
    

    最后附上原文链接:

    NSDataDetector取代iOS的某些正则表达式:URL,电话,日期,地址

    UITextView 识别链接、电话、地址

    相关文章

      网友评论

          本文标题:UITextView 识别链接、电话、地址

          本文链接:https://www.haomeiwen.com/subject/upopsftx.html