merithayan
merithayan

Reputation: 428

How to prevent TextEditor from scrolling in SwiftUI?

I am trying to create a list of TextEditors as part of a SwiftUI app. I want the individual editors not to scroll, as they make up a larger scrolling list. However, when I add the TextEditors to the list, they compress down and each view becomes individually scrollable. Is there a modifier / trick to getting the TextEditor to always fit its text content without scrolling, so I can achieve this?

A minimal example of what I'm trying to do is below:

struct ParentView: View {
    var items: [Item]

    var body: some View {
        List(items, id: \.id) { item in
            EditorView(item: item)
        }
    }
}

struct EditorView: View {
    @ObservedObject var item: Item

    var body: some View {
         TextEditor(text: $item.text)
    }
}

This is in a macOS SwiftUI app, not an iOS one, in case there is any platform differences.

Edit: As pointed out in the comments I tried this approach Dynamic row hight containing TextEditor inside a List in SwiftUI but it didn't seem to work correctly - the rows still didn't expand properly.

Upvotes: 13

Views: 8359

Answers (4)

Xaxxus
Xaxxus

Reputation: 1819

Unfortunately, .scrollDisabled combined with a .fixedSize does not work like it would with a UITextView. Instead it completely breaks the text editor.

I opened a thread about this in the developer forum. Hopefully they fix it soon.

Upvotes: 1

Zach Hall
Zach Hall

Reputation: 71

Will be fixed in macOS 13.0 with a scrollDisabled(_disabled: Bool) function. Seems to be a major hole in TextEditor

https://developer.apple.com/documentation/swiftui/list/scrolldisabled(_:)

Upvotes: 1

XavierZzz
XavierZzz

Reputation: 132

iOS 15 can use in init UITextView.appearance().isScrollEnabled = false

init(para: EditorParagraph) {
      self._para = StateObject(wrappedValue: para)
      UITextView.appearance().backgroundColor = .clear
      UITextView.appearance().textDragInteraction?.isEnabled = false
      UITextView.appearance().isScrollEnabled  = false // here
}

var body: some View{
    ZStack {
        Text(para.content.isEmpty ? para.placeholder : para.content)
            .font(.system(size: para.fontSize, weight: para.content.isEmpty ? .thin : .regular))
            .lineSpacing(6)
            .opacity(para.content.isEmpty ? 1 : 0)
            .padding(.all, 8)
            .frame(maxWidth: .infinity, alignment: .leading)
    
        TextEditor(text: $para.content)
            .font(.system(size: para.fontSize, weight: .regular))
            .foregroundColor(.black)
            .lineSpacing(6)
    
    }
}

Upvotes: 2

merithayan
merithayan

Reputation: 428

To solve this problem, I ended up using the .fixedSize(horizontal: false, vertical: true) modifier to fix the TextEditor to its full size, which then allowed the solution in Dynamic row hight containing TextEditor inside a List in SwiftUI to work as expected within the list.

Upvotes: 5

Related Questions