Ani*_*M H 2 cocoa-touch objective-c ios
在下面的代码中,我正在检测字符串中的url.现在我需要只将超链接放到检测到的"url"并指定UILabel 每当我点击这个url它应该去浏览器,怎么做?
码:
NSString *string = descPost.text;
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray *matches = [linkDetector matchesInString:string options:0 range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches)
{
if ([match resultType] == NSTextCheckingTypeLink)
{
NSURL *url = [match URL];
NSLog(@"found URL: %@", url);
}
}
Run Code Online (Sandbox Code Playgroud)
你应该做的是你应该创建一个UITapGestureRecognizeron UILabel,当UILabel你点击它时,你应该打开网址.
像这样创建手势:
UITapGestureRecognizer *gr =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(myAction:)];
[myLabel addGestureRecognizer:gr];
gr.numberOfTapsRequired = 1;
Run Code Online (Sandbox Code Playgroud)
接下来delegate按下时写下此方法:
- (void) myAction: (UITapGestureRecognizer *) gr {
// write code to open the url here
[[UIApplication sharedApplication] openURL:url];
}
Run Code Online (Sandbox Code Playgroud)