How to enable links in text

前提是你 提交于 2019-12-25 10:39:41

问题


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;  


回答1:


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

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.



来源:https://stackoverflow.com/questions/8466763/how-to-enable-links-in-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!