Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextView not respecting the frame constraint #459

Open
hyouuu opened this issue Nov 23, 2023 · 11 comments
Open

TextView not respecting the frame constraint #459

hyouuu opened this issue Nov 23, 2023 · 11 comments
Assignees
Labels

Comments

@hyouuu
Copy link

hyouuu commented Nov 23, 2023

Screenshot 2023-11-22 at 1 44 37 PM

So when an empty textview is present, typing text that exceeds 1 line works properly (last row), but if I open a textview with text already, it overflows. Thoughts?

My code sample (I'm using my fork to get the height & selected range from https://github.com/hyouuu/SwiftUIX and can submit a PR if you like it)

TextView(
                    "",
                    text: $mo.line,
                    heightToFit: $mo.heightToFit,
                    selectedRange: $mo.selectedRange,
                    onEditingChanged: { didChange in
                        pr("TextView onEditingChanged")
                    }, onCommit: {
                        mo.onReturned?(mo)
                    }
                )
                .interceptReturn(true)
                .onDeleteBackward {
                    pr("TextView onDeleteBackward")
                    mo.onBackspaced?(mo)
                }
                .focused($mo.hasFocus)
                .textContainerInset(EdgeInsets(noteTopInset, 5, 0, 0))
                .font(.system(size: mode.fontSize() + 3))
                .multilineTextAlignment(.leading)
                .frame(height: mo.heightToFit + 13, alignment: .leading)
                .frame(maxWidth: 200)

I also tried setting preferredMaximumLayoutWidth but it still overflows

@vmanot vmanot self-assigned this Nov 24, 2023
@vmanot
Copy link
Member

vmanot commented Nov 24, 2023

@hyouuu thanks for reporting + doing the work of exploring a potential fix! I believe that it's very possible to fix this without introducing a new heightToFit parameter.

Could you isolate and repro this in a sample project? It'd help me debug + fix this fast.

@hyouuu
Copy link
Author

hyouuu commented Nov 25, 2023

Here you go - paste this into a new SwiftUI ContentView with SwiftUIX as a package and you can see the issue:


import SwiftUI
import SwiftUIX

struct ContentView: View {
    @State var path: [String] = []

    @State var lines: [String] = [
        "If you continue typing here, width is correct after 1 line",
        "This is a very long text to showcase the issue with TextView's width constraint that is overflowing",
        "BTW preferredMaximumLayoutWidth doesn't seem to work"
    ]
    
    var body: some View {
        NavigationStack(path: $path) {
            VStack {
                Button {
                   path = ["id"]
                } label: {
                    Text("Tap to show lines")
                }
            }
            .navigationDestination(for: String.self) { id in
                VStack {
                    List {
                        ForEach(0..<lines.count, id: \.self) { i in
                            TextView(
                                "",
                                text: $lines[i],
                                onEditingChanged: { didChange in
                                }, onCommit: {
                                }
                            )
                            .preferredMaximumLayoutWidth(50)
                            .background(Color.systemGray6)
                        }
                    }
                }
            }
        }
    }
}

@hyouuu
Copy link
Author

hyouuu commented Nov 25, 2023

Simulator Screenshot - iPhone 15 Pro - 2023-11-25 at 00 59 32

While making the demo, I found that the 3rd item has correct width when the text width is only slightly taking more than 1 line @vmanot

@hyouuu
Copy link
Author

hyouuu commented Nov 25, 2023

Actually you don't need the NavigationStack at all (I was trying to mimic my implementation) - this shows it:

import SwiftUI
import SwiftUIX

struct ContentView: View {
    @State var path: [String] = []

    @State var lines: [String] = [
        "If you continue typing here, width is correct after 1 line",
        "This is a very long text to showcase the issue with TextView's width constraint that is overflowing",
        "BTW preferredMaximumLayoutWidth doesn't seem to work"
    ]
    
    var body: some View {
        VStack {
            List {
                ForEach(0..<lines.count, id: \.self) { i in
                    TextView(
                        "",
                        text: $lines[i],
                        onEditingChanged: { didChange in
                        }, onCommit: {
                        }
                    )
                    .preferredMaximumLayoutWidth(50)
                    .background(Color.systemGray6)
                }
            }
        }
    }
}

@vmanot
Copy link
Member

vmanot commented Dec 4, 2023

@hyouuu thanks! I'll aim to have this fixed within the next week along with a new release I'm planning to ship.

@hyouuu
Copy link
Author

hyouuu commented Dec 11, 2023

@vmanot that'd be awesome!!

@hyouuu
Copy link
Author

hyouuu commented Dec 18, 2023

@vmanot any updates on this yet?

@vmanot
Copy link
Member

vmanot commented Dec 18, 2023

@hyouuu sorry got sidetracked with a ton of CocoaList refactoring for macOS, will push at least to master tomorrow and ping back on the thread. Thanks for your patience! 🙏

@hyouuu
Copy link
Author

hyouuu commented Dec 19, 2023

No worries at all and thank you for making this library!

@hyouuu
Copy link
Author

hyouuu commented Jan 1, 2024

@vmanot just checking again as I’m getting closer to actually using this. Thanks!

@vmanot
Copy link
Member

vmanot commented Jan 3, 2024

@hyouuu I'm still working on it. I have a fix locally but it's not performing as well as I'd want it to.

If I can't improve over it I'm just going to release it under a user configurable unsafe flag.

@vmanot vmanot added broken-on-latest-release bug Something isn't working labels Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants