79633268

Date: 2025-05-22 07:31:05
Score: 1
Natty:
Report link

In this code, the TextEditor uses .constant(...), which makes the text read-only. This means users can’t type or change the text. If you want to make the text editable, you should use a @State variable instead. This way, the text inside the editor can be updated by the user.

Also i think, some of the paddings can be adjusted.

here is fixed code:

struct TextPageView: View {
  @State private var text: String
  var page: Page
  
  init(page: Page) {
    self.page = page
    // Provide an initial value for the @State variable,
    // so the TextEditor is editable instead of read-only
    _text = State(initialValue: page.content.string ?? "no text")
  }
  
  var body: some View {
    ScrollView {
      Text(text)
      VStack(alignment: .leading) {
        TextEditor(text: $text)
          .font(.system(.body, design: .serif))
          .lineLimit(nil)
          .fixedSize(horizontal: false, vertical: true)
          .multilineTextAlignment(.leading)
          .padding(.trailing, 12)
          .padding(.bottom, 40)
          .background(Color.red) // add background for visibility
      }
    }
    .padding([.leading, .top], 12)
    .navigationTitle(page.content.title ?? "(No Title)")
  }
}

fixed code gif

when your string is nil now you can see "no text"

enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @State
  • Low reputation (0.5):
Posted by: Tornike Despotashvili