79616971

Date: 2025-05-11 22:24:34
Score: 2.5
Natty:
Report link

Inspired by @BenzyNeez`s answer, i've made a small and simple View that:

struct StableHuggingTextfield: View {
    var placeholder: String
    
    @Binding var text: String
    
    private let hPadding: CGFloat = 4
    private let minWidth: CGFloat = 60

    var body: some View {
        Text(text)
            .allowsHitTesting(false)
            .lineLimit(1)
            .truncationMode(.head)
                //Padding to match Text frame to TextField's size
            .padding(.horizontal, hPadding)             
            .padding(.vertical, 5)
            .frame(minWidth: minWidth, alignment: .leading)
            .background { //If overlay, then there is problem when selecting text
                TextField(placeholder, text: $text)
                    .foregroundStyle(.clear)
                    .frame(minWidth: minWidth - hPadding*2)
            }
    }
}

Also, in this code i've added minWidth, as it is required in my project, but it works just fine without it.

Comparison of native, stable and NSViewRepresentable textfield's

However, this approach have some problems:

For my project, these issues are unlikely to occur, so i'm going to stick with this result.

I've also re-tested Auto-Growing NSTextField from this post, that i've mentioned in the question, and it have the same issue, which is fixed with margin, so not ideal. Basically, we're required to do some hacking to have stable TextField, which is sad.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): have the same issue
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: dandand