2023-03-20

How to prevent UITextView from clearing every time when there is a new input?

I am working on a application where I get some data over the network using Mosquito client library and display it on the screen. My problem is that on every new input - say once per second - the old one clears and I get replaced by the new one.

I would like to keep all the text in the field and make the field scrollable once there is no more space in the visible screen.

I am using UITextView to implement that. The UITextView will be non-editable by the user, I just want to display the data received.

What would be the potential solution? I am relatively new to Swift

I have tried the check the different options related to the UITextView class but I couldn't find the proper one.

Here is a simple code and result (sending data from RPi to the swift application):


while 1:

    #publish a message in the certain topic
    client.publish("getData",f"this is message number: {count}\n")
    time.sleep(1)
    count += 1

What I would expect to get is every sentence to be shown with its number on the app screen below the previous one. Once it reaches the bottom line of the UITextView it will continue scrolling forever. Instead every row is replaced by the next one.

display in the UITextView in my Swift app

So, I receive the message using one the Cocoa MQTT delegate methods and if successfully decoded I just display it into the TextView, which is named "displayNMEA", using .text attribute :


    func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
        
        let messageDecoded = String(bytes: message.payload, encoding: .utf8)
        
        if let messageDecodedSafe = messageDecoded {
            displayNMEA.text = messageDecodedSafe
        } else {
            fatalError("Couldn't Decode Message")
        }
     
    }


No comments:

Post a Comment