In SwiftUI, if I have a weblink in between a string, how can I create a hyperlink and have it underlined in my view.
Note, the "messageContent" string won't always be the same.
For example
struct MessageModel {
var messageContent: String = "Test of hyperlink www.google.co.uk within a text message"
}
struct Content: View {
var message: MessageModel
var body: some View {
VStack {
Text(message.messageContent)
}
}
}
An image of what I want to achieve is shown in this image. "www.google.co.uk" is a tappable hyperlink and is underlined
Example of what I want to achieve
UPDATE ON WHAT I"M TRYING TO ACHIVE
I've created the below piece of test code to show what I'm trying to achieve, because as stated above, "messageContent" won't always be the same string.
Whilst the below is not perfect to deal with all cases and handle errors, etc.., this hopefully a better idea of what I'm trying to achieve. Only trouble is this doesn't seem to work.
It produces the underline for the hyperlink, but text doesn't show in Markdown format - see image attached.
import SwiftUI
struct HyperlinkAndUnderlineText: View {
var message: MessagesModel = MessagesModel(messageContent: "Test of hyperlink www.google.co.uk within a text message")
@State var messageContentAfterSplitting: [SplitMessage] = []
var body: some View {
CustomText(inputText: messageContentAfterSplitting)
.onAppear() {
messageContentAfterSplitting = splitMessage(message: message)
}
}
}
struct MessagesModel {
var messageContent: String = ""
}
struct SplitMessage {
var content: String = ""
var type: contentType = .text
}
enum contentType {
case text
case url
}
func splitMessage(message: MessagesModel) -> [SplitMessage] {
func detectIfMessageContainsUrl(message: String) -> [String]? {
let urlDetector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
let matches = urlDetector.matches(in: message, options: [], range: NSRange(location: 0, length: message.utf16.count))
var urls: [String] = []
for (index, match) in matches.enumerated() {
guard let range = Range(match.range, in: message) else { continue }
let url = message[range]
urls.append(String(url))
if index == matches.count - 1 {
return urls
}
}
return []
}
let urlsFoundInMessage = detectIfMessageContainsUrl(message: message.messageContent)
func getComponents(urlsFoundInMessage: [String]) -> [String] {
var componentsEitherSideOfUrl: [String] = []
for (index,url) in urlsFoundInMessage.enumerated() {
componentsEitherSideOfUrl = message.messageContent.components(separatedBy: url)
if index == urlsFoundInMessage.count - 1 {
return componentsEitherSideOfUrl
}
}
return []
}
let componentsEitherSideOfUrl = getComponents(urlsFoundInMessage: urlsFoundInMessage!)
func markdown(urlsFoundInMessage: [String]) -> [String] {
var markdownUrlsArray: [String] = []
for (index, url) in urlsFoundInMessage.enumerated() {
let placeholderText = "[\(url)]"
var url2: String
if url.hasPrefix("https://www.") {
url2 = "(\(url.replacingOccurrences(of: "https://www.", with: "https://")))"
} else if url.hasPrefix("www.") {
url2 = "(\(url.replacingOccurrences(of: "www.", with: "https://")))"
} else {
url2 = "(\(url))"
}
let markdownUrl = placeholderText + url2
markdownUrlsArray.append(markdownUrl)
if index == urlsFoundInMessage.count - 1 {
return markdownUrlsArray
}
}
return []
}
let markdownUrls = markdown(urlsFoundInMessage: urlsFoundInMessage!)
func recombineStrings(componentsEitherSideOfUrl: [String], markdownUrls: [String]) -> [SplitMessage] {
var text = SplitMessage()
var textAsArray: [SplitMessage] = []
for i in 0...2 {
if i.isMultiple(of: 2) {
if i == 0 {
text.content = componentsEitherSideOfUrl[i]
text.type = .text
textAsArray.append(text)
} else {
text.content = componentsEitherSideOfUrl[i-1]
text.type = .text
textAsArray.append(text)
}
} else {
text.content = markdownUrls[i-1]
text.type = .url
textAsArray.append(text)
}
}
return textAsArray
}
let recombinedStringArray = recombineStrings(componentsEitherSideOfUrl: componentsEitherSideOfUrl, markdownUrls: markdownUrls)
return recombinedStringArray
}
func CustomText(inputText: [SplitMessage]) -> Text {
var output = Text("")
for input in inputText {
let text: Text
text = Text(input.content)
.underline(input.type == .url ? true : false, color: .blue)
output = output + text
}
return output
}