detect urls in UILabel

一个人想着一个人 提交于 2019-12-22 06:45:41

问题


I have a Tableview and tableview cell is customized to have a UILabel. The text in UILabel is having URLs. Is there a way to detect urls like how UITextView will enable detect URLs so that user interaction should be able to load the urls.


回答1:


If you just want to identify the URLs, you can use NSDataDetector with the NSTextCheckingTypeLink checking type.

If you want to draw the URLs differently, and you are targeting iOS 6, you can use an NSAttributedString to turn the URLs blue or underline them or whatever. If you're targeting an older version of iOS, you will probably want to look for some free code on the Internet to draw styled text, like OHAttributedLabel.

If you want to actually make the URLs touch-sensitive, you can add a tap gesture recognizer to the label and try to figure out which part of the string was tapped (somewhat complicated), or look for some free code on the Internet that already does it for you (like OHAttributedLabel), or just put a UITextView in the table view cell instead of a label.




回答2:


as Rob point out how can we achieved the same is awesome.

But, we can use a tact so can save with issue of ios version (possibly), just by using a UILabel and UIButton.

What we need to do is that, either from IB or story-board,just place a UILabel with string as URL(use this as title), say;

"www.myURL.com"

Now, just above it, place a UIButton(button with custom type), and just use "______"(underline) and set this button as overlap your UILabel and also the 'underline' must be beneath your label.

Now, just do in action of button whatever you required as you need on the click of URL and here also you can change the textColor, etc, properties; also load URL and navigate to UIWebView.



来源:https://stackoverflow.com/questions/13581714/detect-urls-in-uilabel

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