我有一个UILabel文本 www.google.com 。现在,如果有人将光标移动到文本上并按下它,则应打开谷歌的主页。可能吗?请解释一下。
[emptyLabel setText:@"Sorry it looks like google is not currently available in any of your favourite places nearby, why not ask them to sign up at www.google.com"]
答案 0 :(得分:1)
考虑使用NIAttributedLabel
中的Nimbus project。这是一个自动检测并创建可点击链接的替换UILabel
替换。
答案 1 :(得分:1)
您还可以使用UITextView并启用数据检测器并禁用编辑,从而感受UILabel。具有必要属性集的UItextview将自动检测http链接,电话号码等
如果必须使用UILabel,则需要添加UITapgesture并为点击提供目标。
答案 2 :(得分:1)
// While Loading the view
[self underlineLabel:label withFont:[UIFont systemFontOfSize:17]];
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)];
[label addGestureRecognizer:gr];
gr.numberOfTapsRequired = 1;
//
- (CGFloat)widthOfString:(NSString *)string withFont:(UIFont *)font {
CGSize stringSize = [string sizeWithFont:font];
CGFloat width = stringSize.width;
return width;
}
- (CGFloat)heightOfString:(NSString *)string withFont:(UIFont *)font {
CGSize stringSize = [string sizeWithFont:font];
CGFloat height = stringSize.height;
return height;
}
-(void)underlineLabel:(UILabel *)lbl withFont:(UIFont *)fnt {
float wdth = [self widthOfString:lbl.text withFont:fnt];
float higt = [self heightOfString:lbl.text withFont:fnt];
UIView *viw = [[UIView alloc]initWithFrame:CGRectMake((lbl.frame.size.width - wdth) / 2, (lbl.frame.size.height / 2) + (higt / 2), wdth, 2)];
[viw setBackgroundColor:[UIColor bluecolor]];
[lbl addSubview:viw];
}
// To Add action for gesture recognizer:
- (void)myAction:(UITapGestureRecognizer *)gr {
// Define actions here
}
答案 3 :(得分:0)
您可以使用UTTextView自动检测网址或为网址单独创建按钮。 感谢。