YuAo / WUEmoticonsKeyboard

Customizable emotion icons keyboard for iOS.
MIT License
339 stars 74 forks source link

Not Closed:- show that emoticons in TextView #15

Closed satyamub closed 8 years ago

satyamub commented 8 years ago

Hi @YuAo ... If i will select a Emoticons in in keyboard, I want to show that in TextView... How can I show that? Now its showing only text of the selected emoticons

YuAo commented 8 years ago

See #12

satyamub commented 8 years ago

@YuAo That I know how to do it however I am not able to know where do it!!!

YuAo commented 8 years ago

Display icons in textview is not included in this lib. You need to do it yourself. You can try to fork this project and change the textToInput property of WUEmoticonsKeyboardKeyItem to a NSAttributedString, so that you can assign a text attachment(which can be the image you want to display) to the textToInput. Then modify the inputText: method of WUEmoticonsKeyboard to insert the attributed string textToInput to your text view.

satyamub commented 8 years ago

Hi @YuAo Even I tried that too

however found that [self.textInput replaceRange:range withText:attributedString]; does not accept the attributed string but normal string!!

YuAo commented 8 years ago

@satyamub Right, you need to convert the textInput property to a textview or textfield based on your app's context.

An alternate solution is to use a text view that supports parsing emotion texts, for example, https://github.com/fsjack/JKRichTextView

satyamub commented 8 years ago

If this feature does not work, then there is no use of this library either!!!

satyamub commented 8 years ago

Okay @YuAo ...I am trying !!

YuAo commented 8 years ago

You can also choose not to use this library : )

satyamub commented 8 years ago

@YuAo I did not mean that. I have tried converting the textInput to TextView and then tried to insert the image through the below code

if ([self.textInput isKindOfClass:UITextView.class]) { UITextView textView = (UITextView )self.textInput; NSMutableAttributedString attributedString = [[NSMutableAttributedString alloc] initWithString:@" "]; NSTextAttachment textAttachment = [[NSTextAttachment alloc] init]; textAttachment.image = [UIImage imageNamed:@"weico.png"]; UIImage *myIcon = [WUEmoticonsKeyboard imageWithImage:textAttachment.image scaledToSize:CGSizeMake(20, 20)];

        textAttachment.image = myIcon;

        //I'm subtracting 10px to make the image display nicely, accounting
        //for the padding inside the textView

        NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
        [attributedString replaceCharactersInRange:NSMakeRange(1, 0) withAttributedString:attrStringWithImage];
        textView.attributedText = attributedString;

        }

Its inserting the emoticons however its inserting the image once...

YuAo commented 8 years ago

So, what is your point? It seems that you've successfully inserted the image.

satyamub commented 8 years ago

Yes thats true however when next time I press the image, it replacing all texts with image again. There must be something like cursor needs to be set.

YuAo commented 8 years ago

It is you who replaced all the texts with the image. You just set the entire attributed text of the text view to the attributed string with image attachment.

What you should do is to use the selectedTextRange or selectedRange property of the text view. selectedTextRange/selectedRange can help you find the cursor's current location. You should only replace(or insert to) the "selected" part of the text view's content with your image attached attributed string.

If you don't know what to do, read the documentation, search on google, or head to sites like stackoverflow.

satyamub commented 8 years ago

Thanks sir.. if you could help me in code.. The help will be much appreciated..