如何在SwiftUI中使用超链接实现文本
在Swift中,如图所示,您可以使用“NSMutableAttributedString”在文本中嵌入链接 如何使用SwiftUI实现这一点 我按照如下方式实现了它,但这并不是我想要的如何在SwiftUI中使用超链接实现文本,swiftui,Swiftui,在Swift中,如图所示,您可以使用“NSMutableAttributedString”在文本中嵌入链接 如何使用SwiftUI实现这一点 我按照如下方式实现了它,但这并不是我想要的 import SwiftUI struct ContentView: View { var body: some View { HStack { Text("By tapping Done, you agree to the ") Butt
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
Text("By tapping Done, you agree to the ")
Button(action: {}) {
Text("privacy policy")
}
Text(" and ")
Button(action: {}) {
Text("terms of service")
}
Text(" .")
}
}
}
到目前为止,SwiftUI中没有类似NSAttributedString的东西。
这将暂时解决您的问题:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("By tapping Done, you agree to the ")
HStack(spacing: 0) {
Button("privacy policy") {}
Text(" and ")
Button("terms of service") {}
Text(".")
}
}
}
}
在
UIViewRepresentable
中包装UIKit视图始终是一个选项。只需通过手动过程公开您想要更改的每个属性
struct AttributeText:UIViewRepresentable{
var attributedText:NSAttributedString
init(uAttributeText:NSAttributedString){
self.attributedText=attributedText
}
func makeUIView(上下文:context)->UITextView{
返回UITextView()
}
func updateUIView(\标签:UITextView,上下文:context){
label.attributedText=attributedText
}
}
//用法:AttributeText(NSAttributeString())
使用内置功能+
,它看起来像一个符咒:
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
Button(action: {
}) {
Text("By tapping Done, you agree to the ")
+ Text("privacy policy")
.foregroundColor(Color.blue)
+ Text(" and ")
+ Text("terms of service")
.foregroundColor(Color.blue)
+ Text(".")
}
.foregroundColor(Color.black)
}
}
}
我知道有点晚了,但我用HTML解决了同样的问题。 首先,我创建了一个小助手和链接模型
struct HTMLStringView: UIViewRepresentable {
let htmlContent: String
func makeUIView(context: Context) -> WKWebView {
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.loadHTMLString(htmlContent, baseURL: nil)
}
}
struct TextLink {
let url: URL
let title: String
}
接下来,我创建了一个函数,将字符串更改为HTML,并将第一次出现的@link替换为我的可点击链接
var content = "My string with @link."
var link = TextLink(url: URL(string: "https://www.facebook.com")!, title: "Facebook")
var body: some View {
let bodySize = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body).pointSize
var html = "<span style=\"font: -apple-system-body; font-size:calc(\(bodySize)px + 1.0vw)\">"
if let linkRange = content.range(of: "@link") {
let startText = content[content.startIndex ..< linkRange.lowerBound]
let endText = content[linkRange.upperBound ..< content.endIndex]
html += startText
html += "<a href=\"\(link.url.absoluteString)\">\(link.title)</a>"
html += endText
} else {
html += content
}
html += "</span>"
return HTMLStringView(htmlContent: html)
}
var content=“带@link的我的字符串。”
var link=TextLink(url:url(字符串):https://www.facebook.com),标题:“Facebook”)
var body:一些观点{
让bodySize=UIFont.preferredFont(forTextStyle:UIFont.TextStyle.body)。pointSize
var html=“”
如果让linkRange=content.range(属于“@link”){
设startText=content[content.startIndex..
Swift 5.5
基金会支持降价
Text("[Privacy Policy](https://example.com)")
解释 要创建链接,请将链接文本括在括号中(例如,[Duck Duck Go]),然后立即将URL放在括号中(例如,())
SwiftUI尚不支持任何
属性。只要第一个文本()不超过一行,这就可以工作。现在可以使用链接(“Stackoverflow”),目标:URL(字符串:https://stackoverflow.com”)
在iOS 14和Xcode 12中。为什么这是问题的答案?这不涉及“如何使用属性字符串在swift ui中的文本中嵌入链接”(释义)。同时,这篇文章已经被浏览了超过7k次,答案是上面的评论有0个UpVoTest。这使得整个文本都可以点击,而不仅仅是链接部分。在这种情况下,有两个链接,无法区分单击了哪个链接。这种方法的一个很大的限制似乎是UIViewRepresentable无法将UITextView的内部内容大小传递给SwiftUI-AttributedText将占用它的所有可用空间,这将严重破坏StackView等
My favorite search engine is [Duck Duck Go](https://duckduckgo.com).