swift - 如何使用 NavigationLink 在 SwiftUI 中创建文本(仅文本中的一些单词)
问题描述
我正在为 iOS 开发 SwiftUI 应用程序。我想以这种方式格式化文本,其中蓝色单词应该是 NavigationLinks。文本的外观:
我知道可以将 UIKit 实现到 SwiftUI 代码中。但是,我不明白如何通过正常工作的 NavigationLinks 以这种方式使用 UIKit。
解决方案
使用这个 SO question作为基础,这是一个解决方案。左对齐,但一种方法。
import SwiftUI
struct DynamicLinkTextView: View {
let text: String = "I want text like this, with NavigationLinks to another View. However, this doesn't work"
let wordsWLinks: [String] = ["this", "View"]
@State var selection: String?
var textArray: [String]{
text.components(separatedBy: " ")
}
var body: some View {
NavigationView{
VStack{
MultilineHStack(textArray){ text in
VStack{
if wordsWLinks.contains(text.removePunctiation()){
NavigationLink(text + " ", destination: Text("link = \(text)"), tag: text as String, selection: $selection)
}else{
Text(text + " ").fixedSize()
}
}
}
}
}
}
}
//https://stackoverflow.com/questions/57510093/swiftui-how-to-have-hstack-wrap-children-along-multiple-lines-like-a-collectio
public struct MultilineHStack: View {
struct SizePreferenceKey: PreferenceKey {
typealias Value = [CGSize]
static var defaultValue: Value = []
static func reduce(value: inout Value, nextValue: () -> Value) {
value.append(contentsOf: nextValue())
}
}
private let items: [AnyView]
@State private var sizes: [CGSize] = []
public init<Data: RandomAccessCollection, Content: View>(_ data: Data, @ViewBuilder content: (Data.Element) -> Content) {
self.items = data.map { AnyView(content($0)) }
}
public var body: some View {
GeometryReader {geometry in
ZStack(alignment: .topLeading) {
ForEach(self.items.indices) { index in
self.items[index].background(self.backgroundView()).offset(self.getOffset(at: index, geometry: geometry))
}
}
}.onPreferenceChange(SizePreferenceKey.self) {
self.sizes = $0
}
}
private func getOffset(at index: Int, geometry: GeometryProxy) -> CGSize {
guard index < sizes.endIndex else {return .zero}
let frame = sizes[index]
var (x,y,maxHeight) = sizes[..<index].reduce((CGFloat.zero,CGFloat.zero,CGFloat.zero)) {
var (x,y,maxHeight) = $0
x += $1.width
if x > geometry.size.width {
x = $1.width
y += maxHeight
maxHeight = 0
}
maxHeight = max(maxHeight, $1.height)
return (x,y,maxHeight)
}
if x + frame.width > geometry.size.width {
x = 0
y += maxHeight
}
return .init(width: x, height: y)
}
private func backgroundView() -> some View {
GeometryReader { geometry in
Rectangle()
.fill(Color.clear)
.preference(
key: SizePreferenceKey.self,
value: [geometry.frame(in: CoordinateSpace.global).size]
)
}
}
}
struct DynamicLinkTextView_Previews: PreviewProvider {
static var previews: some View {
DynamicLinkTextView()
}
}
extension String{
func removePunctiation() -> String {
self.trimmingCharacters(in: CharacterSet(charactersIn: ",."))
}
}
推荐阅读
- go - `go list` 没有显示最新版本的包怎么办?
- c++ - 函数签名中未扩展的参数包
- python-3.x - Handling exception in pytest fixture and pytest.fail vs assert in general
- google-apps-script - 将 Stripe API 语法转换为 Google Apps 脚本
- javascript - useCallback:什么时候使用?有什么缺点/性能问题吗?
- python - 尝试使用python多线程excel宏时出错
- react-native - 表单使用 web 但不是 iOS,event.target 返回“未定义”
- java - Java 流 groupingBy 条件
- php - 在 Laravel 中的主模型关系中搜索
- python - 如何调整图像大小并使用 python 保存它们?