In iOS, you can use the NSDataDetector class to determine if a given string is text or a link. Here's an example code snippet:

NSString *string = 'This is a text with a link www.example.com';

NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];

NSArray<NSTextCheckingResult *> *matches = [detector matchesInString:string options:0 range:NSMakeRange(0, string.length)];

for (NSTextCheckingResult *match in matches) {
    if (match.resultType == NSTextCheckingTypeLink) {
        NSURL *url = match.URL;
        NSLog('Link: %@', url.absoluteString);
    } else {
        NSString *text = [string substringWithRange:match.range];
        NSLog('Text: %@', text);
    }
}

In this code, we create an NSDataDetector object using the dataDetectorWithTypes:error: method. This object can detect link types. Then, we use the matchesInString:options:range: method to match links or text within the string. Finally, based on the NSTextCheckingResult object's resultType property, we determine if it's a link or text and handle it accordingly.

Remember that the above code only processes the first match. If your string contains multiple links or text segments, you need to adjust the processing logic as per your requirements.

iOS Objective-C: Detecting Text and Links in Strings

原文地址: https://www.cveoy.top/t/topic/qjjf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录