在SwiftUI中,如果我在一个字符串中有一个网址,那么如何创建一个带下划线的超链接并将其显示在我的视图中。
注意,"messageContent"字符串不总是相同的。
例如:
下面是翻译的结果:
注意,"messageContent"字符串不总是相同的。
例如:
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)
}
}
}
下面是翻译的结果:
我想实现的效果如下图所示。
"www.google.co.uk" 是一个可点击的超链接,并且有下划线。
关于我要实现的更新
我创建了以下代码片段以展示我想要实现的效果,因为如上所述,"messageContent" 字符串不一定相同。虽然以下代码不能处理所有情况并处理错误等内容,但这更好地展示了我尝试实现的内容。唯一的问题是这似乎无法正常工作。
它将为超链接添加下划线,但是文本不以 Markdown 格式显示 - 请参见附加的图像。
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
}