1

I have a custom UILabel. I want to upper-case and bold any URL's that I detect. URL's aren't being upper-cased and bolded. I have a feeling my detection is wrong.

Here is my regex:

static NSRegularExpression *websiteRegularExpression;
static inline NSRegularExpression * WebsiteRegularExpression() {
    if (!websiteRegularExpression) {
        websiteRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|]" 
                                                                        options:NSRegularExpressionCaseInsensitive 
                                                                          error:nil];
    }

    return websiteRegularExpression;
}

Here is the where I do the parsing:

-(void)setBodyText
{
    __block NSRegularExpression *regexp = nil;   
    NSString* labelText = @"http://www.google.com is a cool website";
    [self.bodyLabel setText:labelText afterInheritingLabelAttributesAndConfiguringWithBlock:^NSAttributedString *(NSMutableAttributedString *mutableAttributedString) {

        NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);

        regexp = WebsiteRegularExpression ();
        NSRange nameRange = [regexp rangeOfFirstMatchInString:[mutableAttributedString string] options:0 range:stringRange];
        UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:18.0]; 
        CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
        if (boldFont) {
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:nameRange];
            CFRelease(boldFont);
        }

        if (nameRange.location != NSNotFound)
            [mutableAttributedString replaceCharactersInRange:nameRange withString:[[[mutableAttributedString string] substringWithRange:nameRange] uppercaseString]];
        return mutableAttributedString;
    }];

    regexp = WebsiteRegularExpression();
    NSRange linkRange = [regexp rangeOfFirstMatchInString:labelText options:0 range:NSMakeRange(0, [labelText length])];
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    [self.bodyLabel addLinkToURL:url withRange:linkRange];
}
3
  • What isn't working? What is your question? Commented Jun 29, 2011 at 17:59
  • "I have a feeling my detection is wrong" -OP. The regex looks ok to me. Have you tested the regex? Commented Jun 29, 2011 at 18:19
  • Yeah regex is good actually. Not sure why nothing is getting uppercased and bolded. Commented Jun 29, 2011 at 18:36

1 Answer 1

3

Don't use regular expressions for detecting URLs. Use an NSDataDetector, and then use the -range of the NSTextCheckingResult to bold the range.

Sign up to request clarification or add additional context in comments.

1 Comment

This is just a test. Eventually I am going to detect more complex regexes. I need to be able to get this code down in order to parse different regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.