Ios 如何使字符串列表中的每个元素都可单击?
我有下面的函数,它从Ios 如何使字符串列表中的每个元素都可单击?,ios,swift,button,swiftui,Ios,Swift,Button,Swiftui,我有下面的函数,它从文本中为我提供标签 现在,我正试图让它们中的每一个都像这样可点击,但它不起作用: for element in msg.findHashtags(){ Button(action: { print("go to the hashtags view") }) { Text(element).background(Color("bg")) .foregroundColor(.white)
文本
中为我提供标签
现在,我正试图让它们中的每一个都像这样可点击,但它不起作用:
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())
}
}
以下是autoWordwrapping的扩展模板:
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())
}}
}
struct PositionKey:PreferenceKey{
静态变量defaultValue:[[Int]]=[]
静态函数reduce(值:inout[[Int]],下一个值:()->[[Int]]){
设next=nextValue()
如果next.count==2{value+=[next.first!]}
else{value.replaceSubrange(((value.count-1)…(proxy[value].x-currentPosition){tempCurrentPosition=proxy[value].x
返回[[index]]}
currentPosition=代理[值].x
返回[[索引],]]
}.TransformAnchorReference(键:PositionKey.self,值:。尾部){(currentValue,值:Anchor)位于
如果currentValue.count==1{
如果proxy.size.width<(proxy[value].x-当前位置){
currentPosition=tempCurrentPosition
currentValue=[currentValue.first!,[]}
}
}
}
}.opacity(0.0).overlayPreferenceValue(PositionKey.self){v->AnyView
返回任意视图(VStack(对齐:。前导,间距:10){
ForEach(v,id:\.self){中的行
HStack{
ForEach(行,id:\.self){中的项
TextTag(文本:self.array[项目])
}
}
}
}.不透明度(1.0)
)
}
}
}
}
}
结构文本标记:视图{
变量文本:字符串
var body:一些观点{
按钮(操作:{print(self.text)}){
Text(Text).padding().background(颜色.蓝色)
.foregroundColor(.白色)
.clipShape(胶囊())
}}
}
谢谢,这很有效,但如何将它们水平排序?单击后如何更改按钮的颜色?像堆栈溢出中的upvote按钮?谢谢