ios - 如何让 List of String 的每个元素都可以点击?

标签 ios swift button swiftui

我有下面的功能,它为我提供了来自 texthashtags

现在,我试图让它们中的每一个都像这样可点击,但它不起作用:

for element in msg.findHashtags(){
      Button(action: {
          print("go to the hashtags view")
      }) {
          Text(element).background(Color("bg"))
              .foregroundColor(.white)
              .clipShape(Capsule())
      }
}

最佳答案

答案应该是这样的:

        ForEach( msg.findHashtags(), id: \.self ){element in
Button(action: {
    print("go to the hashtags view")
}) {

    Text(element).background(Color("bg"))
    .foregroundColor(.white)
    .clipShape(Capsule())
}
    }

这是自动换行的扩展模板:

            struct PositionKey : PreferenceKey {
            static var defaultValue  : [[Int]] = []
            static func reduce(value: inout  [[Int]], nextValue: () -> [[Int]]) {
                let next = nextValue()
                if next.count == 2 { value += [next.first!]}
                else { value.replaceSubrange(((value.count - 1) ..< value.count), with:  [value.last! + next[0]])    }
            }
            typealias Value = [[Int]]
        }


        struct TextLink: View {

            let array = ["tage1111111111ddfff11111","tag2","taffffg3","tafffg4","tag4", "taffffg3","tafffg4","tag4","tag4333ddffdd333333333","ta33333333","tag4333333333333",]

        var body: some View {
            var tempCurrentPosition : CGFloat = 0
            var currentPosition : CGFloat = 0

            return ZStack(alignment: .leading){
                    GeometryReader{ proxy in
                        HStack{
                        ForEach( self.array.indices , id: \.self ) { index in
                            TextTag(text: self.array[index]).fixedSize(horizontal: true, vertical: false).anchorPreference(key: PositionKey.self , value: .leading) { (value: Anchor<CGPoint>) in
                                if  currentPosition == 0 { currentPosition = proxy[value].x}
                                if  proxy.size.width > (proxy[value].x - currentPosition) { tempCurrentPosition = proxy[value].x
                                    return [[index]]}
                                currentPosition = proxy[value].x
                                return [[index],[]]
                            }.transformAnchorPreference(key: PositionKey.self, value: .trailing) { ( currentValue, value: Anchor<CGPoint>) in
                                if currentValue.count == 1{
                                    if  proxy.size.width < (proxy[value].x - currentPosition) {
                                        currentPosition = tempCurrentPosition
                                        currentValue = [currentValue.first!, []]}
                                }
                            }
                            }
                    }.opacity(0.0).overlayPreferenceValue(PositionKey.self) { v  -> AnyView in
                        return  AnyView( VStack(alignment: .leading, spacing: 10){
                            ForEach(v , id: \.self){ row in
                                HStack{
                                    ForEach(row , id: \.self){ item in
                                        TextTag(text: self.array[item])
                                    }
                                }
                            }
                        }.opacity(1.0)
                        )
                    }

                    }
                }

                }
                }

        struct TextTag: View {
            var text: String
            var body: some View {
                Button(action:{print(self.text)}){
            Text(text).padding().background(Color.blue)
                .foregroundColor(.white)
                .clipShape(Capsule())
                }}
        }

关于ios - 如何让 List of String 的每个元素都可以点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59365969/

相关文章:

macos - 限制 NSTextField 上的输入

php - Jquery 按钮 OnClick 运行系统命令

android - 当按钮消失时更改按钮 S 的按钮对齐方式

javascript - 无法按字母顺序自动对按钮进行排序

ios - ScrollView 内的 TextView 粘在键盘上

ios - Swift改变了一个 block 的参数类型,这也是Objective-C方法的参数

ios - 这是摆脱 iOS 键盘中的返回按钮的方法吗?

ios - WKWebView 打不开 https ://itunes. apple.com/app/id123456 链接

ios - 我想创建一个允许我的用户删除 UIWebView 历史记录的操作

ios - AVPlayer 和 AVAudioPlayer 不播放 URL 中的音频