ios - SwiftUI 可点击的潜台词

标签 ios swift xcode text swiftui

当点击文本的某些部分时,SwiftUI 中有什么方法可以打开浏览器。

我尝试了上述解决方案,但它不起作用,因为 onTapGesture返回 View您无法添加到 Text

Text("Some text ").foregroundColor(Color(UIColor.systemGray)) +
Text("clickable subtext")
   .foregroundColor(Color(UIColor.systemBlue))
   .onTapGesture {

   }

我想在正文中有可点击的潜台词,这就是为什么使用 HStack不管用

最佳答案

iOS 15 及更高版本更新:
有一个新的Markdown Text 的格式支持, 如:

Text("Some text [clickable subtext](some url) *italic ending* ")
您可以查看WWDC session with a timecode详情
iOS 13 和 14 的旧答案:
不幸的是,SwiftUI 中没有任何类似于 NSAttributedString 的东西。你只有几个选择。 In this answer你可以看看如何使用UIViewRepresentable用于创建老派UILabel with click event , 例如。但是now the only SwiftUI way是使用HStack :
struct TappablePieceOfText: View {
    
    var body: some View {
        
        HStack(spacing: 0) {
            Text("Go to ")
                .foregroundColor(.gray)

            Text("stack overflow")
                .foregroundColor(.blue)
                .underline()
                .onTapGesture {
                    let url = URL.init(string: "https://stackoverflow.com/")
                    guard let stackOverflowURL = url, UIApplication.shared.canOpenURL(stackOverflowURL) else { return }
                    UIApplication.shared.open(stackOverflowURL)
                }
            
            Text(" and enjoy")
                .foregroundColor(.gray)
        }
        
        
    }
}
更新
添加了 UITextView 的解决方案和 UIViewRepresentable .我结合了添加链接的所有内容,结果非常好,我认为:
import SwiftUI
import UIKit

struct TappablePieceOfText: View {
    
    var body: some View {
        TextLabelWithHyperlink()
            .frame(width: 300, height: 110)
    }
    
}

struct TextLabelWithHyperlink: UIViewRepresentable {
    
    func makeUIView(context: Context) -> UITextView {
        
        let standartTextAttributes: [NSAttributedString.Key : Any] = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20),
            NSAttributedString.Key.foregroundColor: UIColor.gray
        ]
        
        let attributedText = NSMutableAttributedString(string: "You can go to ")
        attributedText.addAttributes(standartTextAttributes, range: attributedText.range) // check extention
        
        let hyperlinkTextAttributes: [NSAttributedString.Key : Any] = [
            NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20),
            NSAttributedString.Key.foregroundColor: UIColor.blue,
            NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
            NSAttributedString.Key.link: "https://stackoverflow.com"
        ]
        
        let textWithHyperlink = NSMutableAttributedString(string: "stack overflow site")
        textWithHyperlink.addAttributes(hyperlinkTextAttributes, range: textWithHyperlink.range)
        attributedText.append(textWithHyperlink)
        
        let endOfAttrString = NSMutableAttributedString(string: " end enjoy it using old-school UITextView and UIViewRepresentable")
        endOfAttrString.addAttributes(standartTextAttributes, range: endOfAttrString.range)
        attributedText.append(endOfAttrString)
        
        let textView = UITextView()
        textView.attributedText = attributedText
        
        textView.isEditable = false
        textView.textAlignment = .center
        textView.isSelectable = true
        
        return textView
    }

    func updateUIView(_ uiView: UITextView, context: Context) {}
    
}
HStack 的结果和 Text :
HStack and TextUIViewRepresentable 的结果和 UITextView :
enter image description here
更新 2:
这是一个NSMutableAttributedString小扩展:
extension NSMutableAttributedString {
    
    var range: NSRange {
        NSRange(location: 0, length: self.length)
    }
    
}

关于ios - SwiftUI 可点击的潜台词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59624838/

相关文章:

ios - 获得 FCM token 后 APNS token 是否仍然有效

ios - 在 collectionView 中重用时,UIImageView 大小不适用

iphone - 错误 - Objective-C - 'Application tried to present modally an active controller <splitViewDetailViewController:'

ios - 错误 : Can't assign Value of type '[SomeObject]' to type '[NSMutableArray]'

ios - 快速检测音量变化

xcode - Xcode 6 GM无法在OS X Yosemite上启动

ios - 与 Objective-C 应用程序相比,捆绑的 Swift 库的文件大小开销是多少?

ios - UITableViewController 闪烁行为

ios - 保存和加载自定义字典 ​​- NSUserDefaults

javascript - didSet 在 Javascript : calling function when variable is altered