Just had a thought about a fairly simply way to position the cursor, it’s not perfect but hopefully can give you something to work with.
In main.lua I’ve changed the height param in the table passed into M.digitype to 0:
local options2 = { text = "Second text string Second text string Second text string", x = 100, y = 300, width = 150, -- for multiline text height = 0, -- for multiline text font = native.systemFont, fontSize = 20, color = {1, 1, 1} } local secondFunction= M.digitype (options2)
This still allows multitext (as long as width is set), but allows it to resize itself as it goes. So when there are only 5 characters in the string the height will be 24 pixels for instance, but when there are 15 chars and the text wraps to the next line the text object height will be 48 chars.
For testing, I also added a bit more text to the string just so that I could see the effect for longer.
Now for your function:
local letters = {} local length = string.len( string ) local a = 1 local numLines = nil local lastKnownHeight = 0 local cursorX = myText.x local letterWidth = 0 local newLetter for i = 1, length do table.insert( letters, i, string:sub(i, i)) --print ( letters[i]) end local function typo () myText.text = myText.text..letters[a] if myText.contentHeight \> lastKnownHeight then lastKnownHeight = myText.contentHeight if numLines then numLines = numLines + 1 else numLines = 0 end cursorX = myText.x end newLetter = display.newText( letters[a], x, y, font, fontSize ) letterWidth = newLetter.contentWidth newLetter:removeSelf() newLetter = nil cursorX = cursorX + letterWidth cursor.x = cursorX cursor.y = myText.y + (numLines \* 24) a=a+1 if letters[a] then timer.performWithDelay( math.random(30, 200), typo, 1) end end timer.performWithDelay( math.random(30, 200), typo, 1)
lastKnownHeight is the height of the text field. I track this so that when it changes, I know that the text field has wrapped the text onto a new line:
if myText.contentHeight \> lastKnownHeight then
I then increment the variable numLines by 1 (except on the first call when I initially set it). The reason I set it to 0 for the first line is because it’s really an offset i.e. how many ‘extra’ lines of text are there.
This is then multiplied with another value of your choosing (I just hardcoded 24 but you should use something based on the fontSize I guess) to reposition the y position of the cursor:
cursor.y = myText.y + (numLines \* 24)
Instead of storing increasing substrings, I’ve just stored the individual chars from the string. e.g. {“S”, “e”, “c” …etc} instead of {“S”, “Se”, “Sec” …etc}
Which I add to the text object by taking the existing string and concatenating the new char to it.
myText.text = myText.text..letters[a]
The reason for this is so that I can find out how big each letter is and assign it to the letterWidth variable:
newLetter = display.newText( letters[a], x, y, font, fontSize ) letterWidth = newLetter.contentWidth newLetter:removeSelf() newLetter = nil
In addition to adding the letter to existing text object, I make a new text object with just the next letter, find out how wide it is, and then immediately remove it. Corona will do this so quickly that you will not see the extra text object.
I then use this it move the x position of the cursor:
cursorX = cursorX + (fontSize \* 0.75) cursor.x = cursorX
When the text wrap triggers the contentHeight if statement, I reset cursorX back to the start of the textfield.
cursorX = myText.x
The main issue that remains is that when a word wraps onto a new line, I don’t know how many chars from that word already existed in order to offset the initial cursor x position on the new line.
E.g. if line 1 is in the process of typing the word “string” but runs out of space after typing “stri”, it will move the word down onto the next line.
Line 2 now already has the letters “stri” in it, so you need to offset the x position so that it starts further across.
One thing you could do (if this is all going to be predefined text rather than user input), is break up the lines of text into multiple text objects so that you never see any word wrapping. E.g.
"This is my" --call M.digitype (options1) "very long string" --call M.digitype (options2) "which does not" --call M.digitype (options3) "fit on one line." --call M.digitype (options4)
Hopefully that’s enough to get you going though 
P.S. I typed some of this out this morning, and some during lunch, so there may be the odd typo to look out for.