Code for textfield character limit isn’t working(SwiftUI)

Your solution is lies in SwiftUI’s subscriber .onReceive,

Make sure that your property hasReachedLimit must not marked with @Published else it will trigger infinite loop of view body rendering.

Below shown code works as your expectation.

class TextLimiter: ObservableObject {
    let limit: Int
    @Published var value = ""
    var hasReachedLimit = false
        
    init(limit: Int) {
        self.limit = limit
    }
}

struct Strix: View {
    @ObservedObject var input = TextLimiter(limit: 5)
    var body: some View {
        TextField("Text Input",
                  text: $input.value)
            .border(Color.red,
                    width: $input.hasReachedLimit.wrappedValue ? 1 : 0 )
            .onReceive(Just(self.input.value)) { inputValue in
                
                self.input.hasReachedLimit = inputValue.count > self.input.limit
                
                if inputValue.count > self.input.limit {
                    self.input.value.removeLast()
                }
            }
    }
}

BTW this is not an efficient solution.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top