ios 无法在 UITextView 中使 URL 可点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14387024/
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-08-30 21:54:02  来源:igfitidea点击:

Can't make URL clickable in UITextView

iosurlinterface-builderuitextviewclickable

提问by Madbreaks

I'm using Interface Builder to layout my app. I have a UITextView that contains some text, part of which is a URL that I'd like to make clickable (e.g. launches a browser). I've read up on how to do this and believe I'm doing it correctly, however while the URL appears blue/clickable, clicking it in the iPhone emulator doesn't work. Nothing happens.

我正在使用 Interface Builder 来布局我的应用程序。我有一个包含一些文本的 UITextView,其中一部分是我想让它可点击的 URL(例如启动浏览器)。我已经阅读了如何做到这一点,并相信我做对了,但是当 URL 显示为蓝色/可点击时,在 iPhone 模拟器中点击它不起作用。没发生什么事。

My controller:

我的控制器:

@interface FirstViewController : UIViewController <UISearchBarDelegate>
@property (nonatomic, retain) IBOutlet UITextView *review;
@end

In my implementation I @synthesize review;(and am able to manipulate the view's text, so don't think that's the issue).

在我的实现中,我@synthesize review;(并且能够操纵视图的文本,所以不要认为这是问题所在)。

In IB I have:

在IB我有:

IB Settings

IB设置

..then later when I go to set the text (and attempt to make URLs in the view clickable) I do:

..然后当我去设置文本(并尝试使视图中的 URL 可点击)时,我会这样做:

self.review.text = content;
// My understanding is that this makes URLs clickable...
self.review.dataDetectorTypes = UIDataDetectorTypeLink;

...which displays something like:

...显示如下内容:

Simulator Output

模拟器输出

...which really looks like it wants to work, however when clicking the URL nothing happens. What am I missing?

...看起来它真的很想工作,但是当单击 URL 时什么也没有发生。我错过了什么?

--UPDATE--

- 更新 -

Tried adding self.review.editable = NO;in response to Dixit Patel's answer, but it didn't fix the issue:

尝试添加self.review.editable = NO;以回应 Dixit Patel 的回答,但没有解决问题:

self.review.text = content;
// My understanding is that this makes URLs clickable...
self.review.editable = NO; 
self.review.dataDetectorTypes = UIDataDetectorTypeLink;

采纳答案by Madbreaks

The issue was that User Interaction Enabledwasn't checked at the bottom of the View section of IB's Attributes Inspector.

问题是User Interaction Enabled在 IB 的属性检查器的视图部分的底部没有检查。

回答by IgniteCoders

Check the "Selectable" and "Links" checkboxes in the textview Attributes Inspector:

检查 textview Attributes Inspector 中的“Selectable”和“Links”复选框:

enter image description here

在此处输入图片说明

回答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. 但幸运的是,它们可以通过几行简单的代码启用:

self.review.editable = NO;//required
self.review.selectable = YES;//required
self.review.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,则所有链接都将被视为常规文本。

EDIT

编辑

Have u check properly it may be UITextview disable in your .xib file.

让你正确检查它可能在你的 .xib 文件中禁用了 UITextview。