0

UITextView我开发了一个应用程序,我需要使用必须动态设置高度并且必须识别链接的内容来显示内容。
我使用了上面的代码:

   self.textView.text = [NSString stringWithFormat:@"%@ \n %@", self.offersObjects.body, self.offersObjects.url]; 
   self.textView.dataDetectorTypes = UIDataDetectorTypeLink;   

   if (([[[UIDevice currentDevice] systemVersion] integerValue] < 7)){ 
       CGRect frame = self.textView.frame;   
       frame.size.height =  self.textView.contentSize.height;contentSize.height;              
       self.textView.frame = frame;  
  }else{ 
       [self.textView sizeToFit];  
       [self.textView layoutIfNeeded];  
  }  

我的问题是它无法识别链接。

4

1 回答 1

1

尝试以下代码:

-(IBAction)txtStustes:(id)sender
{
 NSError *error = nil;
 NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink
                            | NSTextCheckingTypePhoneNumber error:&error];

 NSString *string = self.textView.text;
 NSArray *matches = [detector matchesInString:string options:0 range:NSMakeRange(0, [string length])];

for (NSTextCheckingResult *match in matches) {
    if ([match resultType] == NSTextCheckingTypeLink) {
        NSURL *url = [match URL];
        [[UIApplication sharedApplication] openURL:url];
    }         
}
}

还在您的 viewDidLoad 方法中添加以下代码

UITapGestureRecognizer *LblProfileNameTouch=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(txtStustes:)];
[LblProfileNameTouch setNumberOfTouchesRequired:1];
[self.textView addGestureRecognizer:LblProfileNameTouch];
于 2013-11-11T12:31:39.550 回答