0

I want to output a image when a user taps on the keyboard. Let's say the user taps A on the keyboard in a UITextView. Instead of outputting the normal A, I want to output the picture of an Ape. If a user taps "S" I want to output the image of a sun. Is this possible with out having to make a customized keyboard?

  • 1
    http://stackoverflow.com/questions/24010035/how-to-add-image-and-text-in-uitextview-in-ios follow this answer it will help you – Rohit Pradhan Jan 17 '16 at 14:30

1 Answers1

0

Besides creating a custom keyboard yourself, there isn't really a neat method to getting an image output.

One thing you could do is program Swift to automatically recognise the letter input and display an image over or replacing the letter as a result.

To do this, assign a variable to your editable UITextView or UITextField. If you are using XCode you can do this by control-dragging the text field into your code and it will automatically create a weak var, but you can change the strength in the pop up box that appears.

Since you now have a variable, there are two options as to how you display the image. One way is rather boring and will clutter your code, but essentially involves this, which I have simplified for the purposes of demonstration:

if UITextInput == a {
// add image
}

You would have to repeat this for every letter of the alphabet.

Or you could create a dictionary in which each letter corresponds to each image, and find a way to cycle through them, depending on the user's input. Even though this is harder to program for a newbie, it makes the code much neater and quicker.

This is the link to the official Apple documentation on dictionaries:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID113

However, since I assume you are programming an app for the younger market, I would recommend creating a custom keyboard as it will be easier in the long run.

Hope this helps, will

  • Yeah I probably thought so. Okay thank you! I am pretty new to programming, so could you give me any idea, how such a syntax would look? :-) – Morten Birch Jan 17 '16 at 14:52
  • 1
    Hey William. Thank you so much for your help. Appreciate it. I have upvoted, even tho it will first take effect when I have 15 in reputation :(. – Morten Birch Jan 17 '16 at 15:39
  • Hey Will, I have another question if you have time to answer. Where should I place my code? if UITextInput == a { // add image } I tried both in the view did load and in some functions like textViewDidChangeSelection textViewDidBeginEditing but nothing happens. I tried to NSLog("a") just to check. Do you have any suggestions? – Morten Birch Jan 17 '16 at 17:49