SwiftUI可点击的子文本 [英] SwiftUI tappable subtext
问题描述
在SwiftUI中,点击文本的某些部分时,是否可以打开浏览器.
我尝试了上述解决方案,但由于 onTapGesture
返回的 View
无法添加到 Text
Text("Some text").foregroundColor(Color(UIColor.systemGray))+文字(可点击的子文字").foregroundColor(Color(UIColor.systemBlue)).onTapGesture {}
我想在正文中包含可点击的子文本,这就是为什么使用 HStack
无效
不幸的是,在SwiftUI中没有类似于NSAttributedString的东西.而且您只有几个选择.
UIViewRepresentable
和 UITextView
的结果:
更新 2:这是一个 NSMutableAttributedString
小扩展名:
扩展名NSMutableAttributedString {var范围:NSRange {NSRange(位置:0,长度:self.length)}}
Is there any way in SwiftUI to open browser, when tapping on some part of the text.
I tried the above solution but it doesn't work because onTapGesture
returns View
which you cannot add to Text
Text("Some text ").foregroundColor(Color(UIColor.systemGray)) +
Text("clickable subtext")
.foregroundColor(Color(UIColor.systemBlue))
.onTapGesture {
}
I want to have tappable subtext in the main text that's why using HStack
will not work
Unfortunately there is nothing that resembles NSAttributedString in SwiftUI. And you have only a few options. In this answer you can see how to use UIViewRepresentable
for creating an old-school UILabel
with click event, for example. But now the only SwiftUI way is to use 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)
}
}
}
UPDATE
Added solution with UITextView
and UIViewRepresentable
. I combined everything from added links and the result is quite good, I think:
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) {}
}
result of HStack
and Text
:
result of UIViewRepresentable
and UITextView
:
UPDATE 2:
here is a NSMutableAttributedString
little extension:
extension NSMutableAttributedString {
var range: NSRange {
NSRange(location: 0, length: self.length)
}
}
这篇关于SwiftUI可点击的子文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!