Iphone 无法使用正则表达式检测http链接
我正在使用TTtatAttributedLabel检测文本中的超链接,以便使它们可以单击。我的代码不起作用,链接不可点击。事实上,一切都是可以点击的。我只希望URL是可点击的 以下是正则表达式:Iphone 无法使用正则表达式检测http链接,iphone,objective-c,regex,cocoa-touch,nsregularexpression,Iphone,Objective C,Regex,Cocoa Touch,Nsregularexpression,我正在使用TTtatAttributedLabel检测文本中的超链接,以便使它们可以单击。我的代码不起作用,链接不可点击。事实上,一切都是可以点击的。我只希望URL是可点击的 以下是正则表达式: static NSRegularExpression *websiteRegularExpression; static inline NSRegularExpression * WebsiteRegularExpression() { if (!websiteRegularExpression
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;
}
下面是实现
-(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];
}
为什么不仅仅是:
myTextView.dataDetectorTypes=UIDataDetectorTypeLink代码>?为什么不:myTextView.dataDetectorTypes=UIDataDetectorTypeLink
?首先,你说的是http,你的正则表达式包含的内容比http多,其次,尝试这样的方式:https?:/.[^]+
首先,你说的是http,你的正则表达式包含的内容比http多,其次,尝试这样的方式:https?:/.[^]+
我希望能够在我的应用程序内部的自定义视图控制器中打开链接,而不是将用户踢出safari。就在:NSRange linkRange=[regexp rangeofficertmachinstring:labelText选项:0范围:NSMakeRange(0[labelText length]);你能检查一下labelText是否已经发布了吗?labelText字符串似乎已设置为autorelease,因此在设置范围时它可能已消失,因此范围将成为所有内容。使用dataDetectorTypes
属性并将delegate
设置为控制器。然后,在控制器中,您可以实现委托方法attributedLabel:didSelectLinkWithURL:
,以执行任何您想要的操作。我希望能够在我的应用程序内的自定义视图控制器中打开链接,而不是将用户踢出safari。就在:NSRange linkRange=[regexp rangeOfFirstMatchInString:labelText选项:0范围:NSMakeRange(0,[labelText长度])];您可以检查labelText是否已释放吗?看起来labelText字符串已设置为自动释放,因此在您设置范围时它可能已消失,因此范围将成为所有内容。使用dataDetectorTypes
属性并将委托
设置到控制器。然后,在控制器中,您可以实现委托方法attributedLabel:didSelectLinkWithURL:
可以执行任何您想要的操作。