xcode UITextView dataDetectorTypes 无法检测到 http://t.co/ 链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7302707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:52:37  来源:igfitidea点击:

UITextView dataDetectorTypes can't detect http://t.co/ links?

iphoneobjective-cxcodehyperlink

提问by ninjaneer

    theTweet = [[[UITextView alloc] initWithFrame:CGRectMake(65, 10, 225, 65)] autorelease];
    theTweet.text = [[tweets objectAtIndex:index] objectForKey:@"text"];
    theTweet.dataDetectorTypes = UIDataDetectorTypeLink;
    [tweetView addSubview:theTweet];

[[tweets objectAtIndex:index] objectForKey:@"text"]; contains a link with http://t.co/######but it doesn't seem like the UITextView is detecting http://t.colinks. Do I need to use a UIWebView instead?

[[推文 objectAtIndex:index] objectForKey:@"text"]; 包含一个带有http://t.co/######的链接,但 UITextView 似乎没有检测到http://t.co链接。我需要改用 UIWebView 吗?

回答by Brian Sachetta

One thing I've noticed is that in order for UITextViews to recognize links, you need to set selectable to YES. Example:

我注意到的一件事是,为了让 UITextViews 识别链接,您需要将 selectable 设置为 YES。例子:

self.bodyTextView = [[UITextView alloc]initWithFrame:myFrame];
[self.bodyTextView setEditable:NO];
//this is the key
[self.bodyTextView setSelectable:YES];
[self.bodyTextView setDataDetectorTypes:UIDataDetectorTypeLink];
[self.bodyTextView setAttributedText:myAttributedText];

回答by Dixit Patel

Try to use this Hope this help to you

尝试使用这个希望这对你有帮助

Links are not clickable by default in a UITextView. But luckily they can be enabled with a few simple lines of code:

默认情况下,UITextView 中的链接不可点击。但幸运的是,它们可以通过几行简单的代码来启用:

theTweet.editable = NO;
theTweet.dataDetectorTypes = UIDataDetectorTypeLink;

Unfortunately you cannot have an editable UITextViewwith clickable links. If you set editable to YES, then all links will be treated as regular text.

不幸的是,您不能拥有带有可点击链接的可编辑UITextView。如果您将 editable 设置为 YES,则所有链接都将被视为常规文本。

Like this.

像这样。

UITextView *theTweet= [[UITextView alloc] initWithFrame:CGRectMake(65, 10, 225, 65)];
theTweet.text = @"http://t.co/######";
theTweet.editable = NO;
theTweet.dataDetectorTypes = UIDataDetectorTypeLink;
[myview addSubview:theTweet];

回答by user387184

did you set: theTweet.dataDetectorTypes = UIDataDetectorTypeLink; ?

你有没有设置: theTweet.dataDetectorTypes = UIDataDetectorTypeLink; ?

Now that you added that, I tried this code:

既然你添加了它,我尝试了这个代码:

    UITextView *theTweet;
theTweet = [[UITextView alloc] initWithFrame:CGRectMake(65, 10, 225, 65)];
theTweet.text = @"http://t.co/######";
theTweet.editable = NO;
theTweet.dataDetectorTypes = UIDataDetectorTypeLink;
[myview addSubview:theTweet];

and it works fine with me.

它对我来说很好用。

The error must be somewhere else. (did you turn off editable too?)

错误必须在其他地方。(您是否也关闭了可编辑功能?)

回答by Vineet Choudhary

You need to set editable property NO

您需要设置可编辑属性 NO

theTweet = [[[UITextView alloc] initWithFrame:CGRectMake(65, 10, 225, 65)] autorelease];
theTweet.editable = NO; //add this line
theTweet.text = [[tweets objectAtIndex:index] objectForKey:@"text"];
theTweet.dataDetectorTypes = UIDataDetectorTypeLink;
[tweetView addSubview:theTweet];

回答by Renetik

Maybe good to make extension so we don't have to memorize it...

做扩展可能很好,这样我们就不必记住它了……

@implementation UITextView (Extension)

- (instancetype)dataDetector :(UIDataDetectorTypes)types {
    self.dataDetectorTypes = types;
    if (types != UIDataDetectorTypeNone) self.selectable = true;
    return self;
}

@end