无法使用 SwiftUI 在列表中显示整个字符串

Can't show the whole string in a list using SwiftUI

当你打开 sample project:

先按一行,然后会出现一个加号按钮。点击加号按钮一次后,该行将附加一个加号。如果我多次点击按钮,++++ 字符串将显示 +...(如下图所示)。我没想到这一点,我希望行高会自动增加,以便可以显示整个字符串。我应该怎么做才能解决它?

如果我点击加号按钮 5-6 次,行高将按预期自动增加。

主要项目代码在sample project:

ContentView.swift

struct ContentView: View {
    
    @EnvironmentObject var userData: UserData
    
    var body: some View {

        HStack {
            List {
                ForEach(userData.subtasks) { subtask in
                    SubtaskRowNextToCard(subtaskModel: subtask)
                }
            }
            
            if userData.currentSubtask != nil {
                SubtaskCard()
                    .padding(.all)
            }
        }
        .onAppear {
                        
            for _ in 0..<20 {
                appendASubtask()
            }
            
        }
        
    }
    
    func appendASubtask() {
        let aSubtask = SubtaskModel(
        score: ""
        )
        
        userData.subtasks.append(aSubtask)
    }
    
}

SubtaskModel.swift

import Foundation
import SwiftUI

class SubtaskModel: ObservableObject, Identifiable {
    @Published var score: String = ""
    
    init(
        score: String
    ) {
        self.score = score
    }
}

SubtaskRowNextToCard.swift

struct SubtaskRowNextToCard: View {
    
    @ObservedObject var subtaskModel: SubtaskModel
    @EnvironmentObject var userData: UserData
    
    @State var scoreWithRound: String = ""
    
    var body: some View {
        
        Button(action: {
            userData.currentSubtask = subtaskModel
        }) {
            Text(scoreWithRound)
        }
        .onAppear {
            updateScore()
        }
        .onReceive(NotificationCenter.default.publisher(for: NSNotification.Name.init("changedCurrentSubtask"))) { obj in
            updateScore()
        }
        
    }
    
    func updateScore() {
        scoreWithRound = subtaskModel.score
    }
    
}

SubtaskCard.swift

struct SubtaskCard: View {
    
    @EnvironmentObject var userData: UserData
        
    var body: some View {
        
        Button(action: {
            print("+ button was tapped")
            appendScore(newScore: "+")
        }) {
            Image(systemName: "plus.circle.fill")
        }
        .buttonStyle(PlainButtonStyle())
        
    }
    
    func appendScore(newScore: String) {
        if let subtaskModel = userData.currentSubtask {
            subtaskModel.score = subtaskModel.score + newScore + " "
                        
            NotificationCenter.default.post(name: NSNotification.Name.init("changedCurrentSubtask"), object: nil, userInfo: nil)
        }
    }
}

添加 .fixedSize(horizontal: false, vertical: true) 它不会再截断文本:

  ForEach(userData.subtasks) { subtask in
                    SubtaskRowNextToCard(subtaskModel: subtask).fixedSize(horizontal: false, vertical: true)
                }

编辑: 对于动态高度修复:

Text 替换为 TextEditor,您将不会注意到闪烁:

Text(scoreWithRound)

TextEditor(text: $scoreWithRound).allowsHitTesting(false)