Url 如何以编程方式检测uilabel上的链接?
我想从UILabel检测像www.google.com这样的URL。我该怎么做 我已经制作了一个文本字段,其中输入的内容将显示在标签上。我想从这个文本中检测URl。请帮忙Url 如何以编程方式检测uilabel上的链接?,url,hyperlink,uilabel,Url,Hyperlink,Uilabel,我想从UILabel检测像www.google.com这样的URL。我该怎么做 我已经制作了一个文本字段,其中输入的内容将显示在标签上。我想从这个文本中检测URl。请帮忙 (void)displayMethod:btnOk { name = textField.text; label = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 280, 80)]; label.backgroundColor = [UICol
(void)displayMethod:btnOk
{
name = textField.text;
label = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 280, 80)];
label.backgroundColor = [UIColor clearColor];
label.textColor=[UIColor blackColor];
label.text = name;
label.numberOfLines=0;
[self.view addSubview:label];
如果传递的字符串不包含有效的URL,NSURL的URLWithString将返回nil。因此,只需检查标签文本字符串的返回值,查看URL是否有效 下面是一些示例代码
if([NSURL URLWithString:label.text] == nil){
//sring inside the label is not a valid url
}
else {
//string inside is a valid url
}
//If you want to disable highlights, and hyperlinks it is possible to do
//it with UITextViews by doing something like this
myTextView.dataDetectorTypes = UIDataDetectorTypeNone;
@user3193017您问我们如何从给定字符串检测url,上面的答案回答了这个问题。如果您还想禁用标签上显示的链接,请更新您的问题。现在我将为您更新我的答案,其中包括禁用超链接:D