作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 SwiftUI 中,如果我在字符串之间有一个网络链接,我该如何创建一个超链接并在我的 View 中为其添加下划线。
请注意,“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”是一个可点击的超链接,并带有下划线
Example of what I want to achieve
更新我正在努力实现的目标
我创建了下面的一段测试代码来展示我想要实现的目标,因为如上所述,“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
}
最佳答案
在 Swift 5.5 (iOS 15+) 中
你可以使用 Markdown :
Text("This is a link [Google](https://google.com.com)")
要只在链接上添加下划线,您可以这样做:
Text("This is a link ") + Text("[google.com](https://google.com.com)").underline()
关于SwiftUI 文本 - 如何创建超链接并在字符串中为网络链接下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71541408/
我是一名优秀的程序员,十分优秀!