- Home /
Question by
Brains · Oct 08, 2012 at 07:40 AM ·
iosinputkeyboardmobile keyboard
iOS keyboard - adding and removing characters in a string on the fly
I am trying to replicate the following functionality I have working on a PC keyboard, shown below, on a mobile device using iOS.
foreach (char c in Input.inputString)
{
// if backspace
if (c == "\b"[0])
{
if (answerTextMesh.text.Length != 0)
{
// if pos < the lengthe of the word delete character and add underscores, else just delete character
if (pos <= wordSet[0].Length)
{
if (pos > 0)
{
pos -= 1;
}
answerTextMesh.text = answerTextMesh.text.Remove(pos, 1);
answerTextMesh.text = answerTextMesh.text.Insert(pos, "_");
}
else
{
if (pos > 0)
{
pos -= 1;
}
answerTextMesh.text = answerTextMesh.text.Substring(0, answerTextMesh.text.Length - 1);
}
answerTextMesh.Commit();
}
}
else
{
if (answerTextMesh.text.Length < 12)
{
// remove underscores if typing still less than word length
if (pos < wordSet[0].Length)
{
answerTextMesh.text = answerTextMesh.text.Remove(pos, 1);
}
answerTextMesh.text = answerTextMesh.text.Insert(pos, c.ToString());
answerTextMesh.Commit();
// increase value of pos per character
pos += 1;
}
}
}
The code is filling a string/text variable with underscores depending on the word length. Then when a character is typed, replaces the first one and so on. Then when backspace is hit removes the character from the current end position and reinserts an underscore at that spot.
I have tried a few alternatives of this method to work with the iOS keyboard, but haven't had any astounding amount of success.
If anyone has any advice or tips that would be amazing. If more info is needed let me know.
Comment