2

I have a string like this,

var str = "Hello, go to http://stackoverflow.com"

and I want to display this string in a UIWebView, so that http://stackoverflow.com will be displayed as a link.

To do this, I am using loadHtmlString of UIWebView.

But the text is not displayed as a link. It is displayed as a normal text in UIWebView.

After some research, I found that the issue is missing of <a href = ""></a> tag.

For a static string, I can manually add the tags to the string. But I am getting the string from a http response. So I have no idea how to add the <a> tags in appropriate indices.

Is there any way I could parse and display the text as a link in UIWebView?

  • 2
    Is it required that the string is displayed in a `UIWebView`? You could display it in a `UITextView` with editing disabled and link detection enabled – ABC Feb 03 '16 at 14:04
  • 1
    Yes.. Because I am displaying images also. – Deepika Masilamani Feb 03 '16 at 14:07
  • 2
    @DeepikaMasilamani Then I recommend using regular expressions (`NSRegularExpression`) and replacing the matched strings with the string wrapped by the `` tag – ABC Feb 03 '16 at 14:24
  • 1
    Take a look at this question http://stackoverflow.com/questions/9587458/using-nsregularexpression-to-extract-urls-on-the-iphone – ABC Feb 03 '16 at 14:27
  • 1
    Thanks for you suggestion. Can I get any sample code to use NSRegularExpression? I am not used to `NSRegularExpression`. That is why, If I get any links to learn about it, then it would be helpful to me. – Deepika Masilamani Feb 03 '16 at 14:27
  • 1
    Thank you so much @AlexanderNorth for suggesting me a link. I will definitely take a look at it. – Deepika Masilamani Feb 03 '16 at 14:28
  • Tell the server team to deliver the response string having `href` tag. Editing the content may be become a messy.. – NSPratik Feb 03 '16 at 14:29
  • 1
    okay @NSPratik.. will definitely consider your suggestion. Thank you. – Deepika Masilamani Feb 03 '16 at 14:30
  • 1
    @DeepikaMasilamani I can write some sample code in a few minutes if that would help – ABC Feb 03 '16 at 14:32

2 Answers2

4

Set the dataDetectorTypes to Link on the UIWebView. Here is an example you can run and view in a playground:

import UIKit
import XCPlayground

let str = "Hello, go to http://stackoverflow.com"
let webview = UIWebView(frame: CGRect(x: 0, y: 0, width: 200.0, height: 200.0))
webview.dataDetectorTypes = .Link
webview.loadHTMLString(str, baseURL: nil)

XCPlaygroundPage.currentPage.liveView = webview

If your content does not rely on any HTML features and you are just tyring to display links then you should use a UITextView which also supports dataDetectorTypes.

Joe
  • 56,979
  • 9
  • 128
  • 135
2

@Joe's answer also looks good, here is another solution

let str = "Hello, go to http://stackoverflow.com or to http://example.com "

let detector = try! NSDataDetector(types: NSTextCheckingType.Link.rawValue)

let matches = detector.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))

var newStr = str
for match in matches {
    let url = (str as NSString).substringWithRange(match.range)
    newStr = newStr.stringByReplacingOccurrencesOfString(url, withString: "<a href='\(url)'>\(url)</a>")

}

print(newStr)

This modifies the string, to then be used in the UIWebView

ABC
  • 718
  • 8
  • 23