If you (like me) have struggled with dismissing the keyboard when the return key is pressed in a multiline #SwiftUI text field, this article shows you how to easily achieve this with a FocusState and an onChange view modifier.
https://danielsaidi.com/blog/2023/09/15/dismiss-keyboard-when-return-is-pressed-in-a-multiline-swiftui-textfield
@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.