如何在文本中启用链接 [英] How to enable links in text

查看:112
本文介绍了如何在文本中启用链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实现来显示文本。但是,我无法点击www.google.com文本中的链接。如何更改我的实现,如果存在链接,我将能够点击它并将其重定向到网页?

I have the following implementation to display the text. However, I am unable to click on a link in the text "www.google.com". How can I change my implementation such that if a link exists, I will be able to click on it and it redirects me to the web page?

NSString *answer = [NSString stringWithFormat:@"%@",self.answerForCell.text];
CGFloat answerLabelHeight = [CustomCell getHeightOfLabel:answer ofFontSize:ANSWER_FONT_SIZE withConstraint: ANSWER_CONSTRAINT];
UILabel *thisAnswerLabel = (UILabel*)[self.contentView viewWithTag:ANSWERLABEL_TAG];
[thisAnswerLabel setFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN + CELL_AVATAR_WIDTH + CELL_SPACING, currentYAxisValue, CELL_ANSWER_WIDTH, answerLabelHeight)];
thisAnswerLabel.text = answer;  

推荐答案

UILabels不支持超链接。请考虑使用 UIWebView 在答案中显示文字。

UILabels do not support hyperlinking. Instead consider using a UIWebView to display the text in the answer.

另一种方法是使用正则表达式检查文本是否包含链接,在这种情况下将整个标签转换为用户的按钮可以点击/点按。对我而言,这似乎是一个首选Web视图的设置,因为我猜测答案可能是一个很长的文本。

Another way to do it would be to use regular expressions to check if the text contains a link, and in that case turn the whole label into a button that the user can click/tap. To me this seems like a setting where a web view is preferred though, since I'm guessing the answer could be a long text.

这篇关于如何在文本中启用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆