UItextView dynamiclly高度和链接重新配置iOS 6与ios 7

时间:2013-11-11 12:26:04

标签: ios6 ios7

我开发了一个应用程序,我需要使用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]; } 必须动态设置高度,并且必须识别链接。
  我使用了上面的代码:

{{1}}

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

1 个答案:

答案 0 :(得分: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];