0

I have been struggling with this. User needs to enter text and/or emoticons in a TextView. I got an emoticon keyboard with my own images to enter emoticons. Problem is I need to keep a symbol (e.g. "(smile)" for the emoticon within the text while AT THE SAME TIME showing the emoticon picture on top of the symbol.

So user would see "Hello [the picture]" while the TextView.text property would return "Hello (smile)".

On Android you can use Spanned strings which allow you to cover part of your text with an image. Thus on Android I managed to achieve my objective without problem.

On iOS, I thought Attributed Strings were a similar concept to Spanned but so far all I have been able to do is entirely replace the emoticon's code with the picture (using NSTextAttachment). Is there a way to achieve my objective without having to maintain one attributed string containing pictures and one separate string containing codes?

Georges
  • 307
  • 3
  • 13
  • Hey Georges be more specific.. your question have double meaning or hard to understated. – Bevin Patel Apr 16 '15 at 05:41
  • Hi Bevin, updated my post, hope it is clearer. – Georges Apr 16 '15 at 06:02
  • I am not getting your query actually what you want in output.. – Bevin Patel Apr 16 '15 at 06:12
  • Sorry I don't know how to make it clearer... As output I want textview to show text and images (so the attributedtext should include those images) and I also want the text property of the textview to include text and emoticon codes (e.g. (smile), or (123), etc...) – Georges Apr 16 '15 at 06:18
  • Sorry George, I got what ever you want to ask... Technically this is possible, but its very hard to mange . You have to make your own classes to draw text and image onUiViewContext. and You can do using override your class from UIView. – Bevin Patel Apr 16 '15 at 06:31
  • I want the user to see my own custom images inline with the text. And I want the corresponding custom symbols to be included in the text returned by textView.text. – Georges Apr 16 '15 at 06:31
  • Hey Georges .. This may be helps you and your asking way should be like this.. "http://stackoverflow.com/questions/16707948/creating-a-good-custom-emoticon-chat-experience-on-ios " – Bevin Patel Apr 16 '15 at 06:38
  • Use this linke to solve your problem https://github.com/YuAo/WUEmoticonsKeyboard – Bevin Patel Apr 16 '15 at 06:41
  • I This might help you. http://stackoverflow.com/questions/20930462/ios-7-textkit-how-to-insert-images-inline-with-text – Vikas Dalvi Apr 17 '15 at 10:47

2 Answers2

0

You can use this method, Hope it will work for you.

- (NSAttributedString*) parseEmoticons:(NSAttributedString*)text {

            text = [text stringByReplacingOccurrencesOfString:@":-)" withString:@""];
            text = [text stringByReplacingOccurrencesOfString:@";P" withString:@""];
            text = [text stringByReplacingOccurrencesOfString:@"B-)" withString:@""];
            text = [text stringByReplacingOccurrencesOfString:@";-P" withString:@""];

            return text;
    }
Avineet Gupta
  • 586
  • 10
  • 21
  • Thanks Avineet. Problem is this solution replaces codes (strings) with 1-character emoticons (strings). I need to use my own emoticons (pictures) and keeping the codes below. – Georges Apr 16 '15 at 05:54
0

Having failed to find a more elegant solution, I resorted to maintaining one attributedstring containing the emoticon picture, and one regular string to hold the emoticon codes. So my attString is for instance "Hello [Smiling picture]" while my string is "Hello %101%". If you are interested in building a chatting app as I am, here is the pseudo code:

In emoticon keyboard:

{

Insert picture into attributed string at location loc;

Call textView shouldChangeTextInRange:(loc,0) replacementText:"%101";

}

In the view controller at shouldChangeTextInRange:(loc,length) replacementText:text:

{

Parse regular string to jump over emoticon codes already there to find the location matching loc;

Replace text (for instance %101%) in regular string} at the identified location.

}

Note: shouldChangeTextInRange is also called for regular keyboard entries including delete.

Georges
  • 307
  • 3
  • 13