I use TTTAttributedLabel to detect hyperlinks in my text so that I can make them clickable. My code does not work, and links are not available. In fact, everything is viewable. I want the URL to be accessible only to the client.
Here is the 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 implementation
-(void)setBodyText { __block NSRegularExpression *regexp = nil; NSString* labelText = [[message valueForKey:@"body"]gtm_stringByUnescapingFromHTML]; //@"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(); [regexp enumerateMatchesInString:[mutableAttributedString string] options:0 range:stringRange usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:18.0]; CTFontRef italicFont = CTFontCreateWithName((CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL); if (italicFont) { [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)italicFont range:result.range]; CFRelease(italicFont); [mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:result.range]; } }]; 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]; [Utils alignLabelWithTop:bodyLabel]; }
Sheehan alam
source share