mastodon.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
The original server operated by the Mastodon gGmbH non-profit

Administered by:

Server stats:

366K
active users

@danielsaidi Nice work. I have a variation of this that also supports pasting text. And if that text contains \n then I replace them with a space (and don’t lose focus). So hacky, but it is what SwiftUI asks of us. 🤪

@rob Nice! I should adjust my code so that only trailing newlines are removed and then do like you do, replace inline newlines with spaces 👍

@danielsaidi Aaah, not so fast. What would happen if the user were to move their cursor to the beginning of the TextField and then hit return? 🫠
My best attempt counts characters, and only lose focus if there was one char added and that char is \n. Anything else, replace all \n with a space.

@rob Haha yeah, you're right 😅 Having the dedicated modifier is great, since it can just wrap that kind of logic and state in a way that just having a view extension can’t.