Rich Text Imput on iOS
Join the DZone community and get the full member experience.Join For Free
rich text input is difficult on any platform, but i discovered a rather cheeky, but simple trick to give you rich text inputs on ios.
the way a number of rich text input libraries work (like codemirror and ace), is that when you're typing, you're typing in to a tiny, almost hidden, textarea . this value is then echoed and rendered using styled spans and the like creating a rich mirror of your input. of course the editors are much more complicated than that - but our ios technique follows this idea really closely.
basically, in ios if you set an input or textarea to have an opacity of zero, you can't see it (obviously!). however when you focus the element, the cursor is fully visible.
also, selection still work, though it only shows the selection colour and block, not the text.
so what we do is sit a textarea on top of a div , listen for keyup , and take the value in the textarea , run it through a parser to get something syntax highlighting and place the rendered html under the textarea . this gives the illusion that we have a rich text input element.
you could use this trick to allow the user to select and highlight certain parts of a document - just like you can in the mobile version of google docs in edit mode.
here's a demo that you can try out on an ios device, which when you type will syntax highlight the code: example rich text input for ios
finally, if you want to learn more about mobile development, my conference full frontal, is running a full 2 day masterclass with the lord of mobile: ppk , so check it out (including ticket to the conference & exclusive dinner with the workshop teachers).
Published at DZone with permission of Remy Sharp, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.