如果文本为 URL SwiftUI,如何突出显示和可点击

How to Highlight and Clickable if Text is URL SwiftUI

如果聊天屏幕中的文本包含URL,是否有任何方法可以突出显示文本并可点击

发件人用户代码:-

  import SwiftUI
  struct TextHighlightURL: View {
  var body: some View {
    HStack(alignment: .bottom){
        Spacer()
        Text("2:22 PM").font(.system(size: 10))
            .foregroundColor(.gray)
        HStack {
            Text("www.google.com")
                .foregroundColor(.white)
                .multilineTextAlignment(.trailing)
                .padding(10)
          }
          .background(Color.init("DTR-ChatSendrColor"))
          .cornerRadius(10.0)
         }.padding(.vertical,5)
         .padding()
      }
  }

输出:-

接收者用户代码:-

struct SenderReciverUI1: View {
var body: some View {
    Group {
        HStack(alignment: .bottom){
            VStack(alignment: .leading,spacing:5) {
                HStack(alignment: .bottom) {
                    Text("www.google.com")
                        .foregroundColor(.white)
                        .padding(10)
                        .cornerRadius(10.0)
                   }
              }
             Spacer()
        }.padding(.vertical,5)
        .padding()
       }
     }
 }

我的目标:-

有人可以向我解释一下如果发件人发送 link 并且接收者收到 link 如果文本包含 [=43= 则它会自动突出显示并且可点击], 我已经按照上面的方法实现了,还没有结果。

如有任何帮助,我们将不胜感激。

提前致谢。

您需要为 TextView 创建自定义 UIViewRepresentable

检查下面的代码,这可能对您有所帮助。

struct TextView: UIViewRepresentable {
    
    @Binding var text: String
    @Binding var textStyle: UIFont.TextStyle
    
    func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        
        textView.delegate = context.coordinator
        textView.font = UIFont.preferredFont(forTextStyle: textStyle)
        textView.autocapitalizationType = .sentences
        textView.isSelectable = true
        textView.isUserInteractionEnabled = true
        textView.isEditable = false
        textView.dataDetectorTypes = .link
        
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        uiView.text = text
        uiView.font = UIFont.preferredFont(forTextStyle: textStyle)
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator($text)
    }
    
    class Coordinator: NSObject, UITextViewDelegate {
        var text: Binding<String>

        init(_ text: Binding<String>) {
            self.text = text
        }
        
        func textViewDidChange(_ textView: UITextView) {
            self.text.wrappedValue = textView.text
        }
    }
}

在您的 “接收方用户代码:-”“发送方用户代码”相同

struct SenderReciverUI1: View {
@State private var message = "Hello, www.google.com. this is just testing for hyperlinks, check this out our website https://www.apple.in thank you."
@State private var textStyle = UIFont.TextStyle.body

var body: some View {
    Group {
        HStack(alignment: .bottom){
            VStack(alignment: .leading,spacing:5) {
                HStack(alignment: .bottom) {
                    TextView(text: $message, textStyle: $textStyle)
                        .foregroundColor(.white)
                        .padding(10)
                        .cornerRadius(10.0)
                   }
              }
             Spacer()
        }.padding(.vertical,5)
        .padding()
       }
     }
 }

如果您需要什么,请告诉我。