Last active
August 29, 2021 00:20
-
-
Save shohiebsense/495e4f7fa6262e9a746fae0d59159653 to your computer and use it in GitHub Desktop.
Prevents Multiple Spaces And Auto Capitalization On TextField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val text = mutableListOf("") | |
fun onValueChanged(value: String) { | |
if (!value.isName()) return | |
val filteredValue = value.replace("\\s+".toRegex(), " ") | |
if (value.contains("\n")) return | |
text.value = value | |
} | |
fun onFocusChanged(isFocused: Boolean) { | |
val newValueList= text.value.trim().splitToSequence(' ').filter { it.isNotBlank() }.toMutableList() | |
newValueList.forEachIndexed { i, word -> | |
var newWord = "" | |
word.forEachIndexed { index, c -> | |
newWord += if(index == 0) c.uppercase() | |
else c.lowercase() | |
} | |
newValueList[i] = newWord | |
} | |
text.value = newValueList.joinToString(separator = " ") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment