9

I am working on iOS 8 custom keyboard, where i have designed keyboard using some images like smiley. i want this keyboard to be work with iMessage. when i am trying to send text its working properly but can't able to share image there. I have tried following code :

To share text : (its working properly)

-(void)shouldAddCharector:(NSString*)Charector{
    if ([Charector isEqualToString:@"Clear"]) {
        [self.textDocumentProxy deleteBackward];
    } else if([Charector isEqualToString:@"Dismiss"]){
        [self dismissKeyboard];
    } else {
        [self.textDocumentProxy insertText:Charector];
    }
}

To add image : ( Not working)

-(void)shouldAddImage:(UIImage*)oneImage
{
        UIImage* onions = [UIImage imageNamed:@"0.png"];

        NSMutableAttributedString *mas;
        NSTextAttachment* onionatt = [NSTextAttachment new];
        onionatt.image = onions;
        onionatt.bounds = CGRectMake(0,-5,onions.size.width,onions.size.height);
        NSAttributedString* onionattchar = [NSAttributedString attributedStringWithAttachment:onionatt];

        NSRange r = [[mas string] rangeOfString:@"Onions"];
        [mas insertAttributedString:onionattchar atIndex:(r.location + r.length)];
        NSString *string =[NSString stringWithFormat:@"%@",mas];
        [self.textDocumentProxy insertText:string];
}

Is there any possibility to pass image to [self.textDocumentProxy insertText:string];

following attached image shows how exactly i want to use this image keyboard. i am surprised how emoji keyboard will work? enter image description here

Andrew
  • 15,357
  • 6
  • 66
  • 101
Gaurav
  • 8,227
  • 4
  • 34
  • 55
  • is there anyway to convert my attributedstring(image) to NSString format so i can atatch images... – Sri Oct 21 '14 at 11:31
  • @Gaurav: did you find anything on this? – Manthan Mar 25 '15 at 08:01
  • @Manthan I have done in a way that matt suggested below. You can check performance by downloading hi-art app from appstore. – Gaurav Mar 26 '15 at 12:06
  • Follow this answer. http://stackoverflow.com/questions/29426098/ios-custom-keyboard-i-want-to-send-images-to-the-textdocumentproxyinput-contr – Ramdhas Jun 16 '15 at 16:23
  • Do you have any solution for this @Gaurav, Please help me with the same issue. Thanks. – Cu'i Bap Feb 27 '20 at 04:02

1 Answers1

9

As far as I know, the behavior you are looking for is not possible as of iOS 8 beta 4.

Currently, the only way for iOS custom keyboards to interact with text is through <UITextDocumentProxy> and the only way to insert anything is via the insertText: method.

Here is the header for the insertText: method in <UITextDocumentProxy>:

- (void)insertText:(NSString *)text;

As you can see, it takes a plain NSString... not an NSAttributedString. This is why your attempt to insert an image doesn't work.

However, despite the fact that you can't add pictures, it is still very possible to insert emojis, since an emoticon is really just a Unicode character.

To add an emoji, just insert the proper Unicode character:

[self.textDocumentProxy insertText:@"\U0001F603"];

Useful links:

List of Unicode Emoji: http://apps.timwhitlock.info/emoji/tables/unicode

Unicode Characters as NSStrings: Writing a unicode character with NSString

Community
  • 1
  • 1
Matt
  • 1,359
  • 18
  • 22
  • Thanks matt, i have already done with emoji adding to keyboard now just looking for a workaround to add image. – Gaurav Jul 31 '14 at 03:28
  • 1
    There is no work around. Unfortunately, it's simply not possible. – Matt Jul 31 '14 at 08:25
  • 1
    is there anyway to convert my attributedstring(image) to NSString format so i can atatch images. – Sri Oct 20 '14 at 08:49
  • 4
    Only way to achieve this is copy that image to clipboard and user will manually paste it. – Gaurav Nov 13 '14 at 12:56
  • Has this feature been added yet? iOS 8.3 is out, can we do this now? – mcottingham Mar 17 '15 at 02:30
  • @Matt: Then how to do something similar like this app: https://itunes.apple.com/us/app/rage-faces-keyboard/id924790727?mt=8&ign-mpt=uo%3D4 – Manthan Mar 25 '15 at 08:01
  • 1
    It works by placing the image on the user's clipboard, and requires the user to paste it in, as Gaurav said above. Many custom keyboards with images are being implemented in this way, and as of iOS 8.3, there is still no better way to do it. **Please be sure to file a feature request with Apple via the bug reporter.** – macserv Apr 21 '15 at 20:54
  • What about the Viber, Facebook and other apps? copy pasting is just a hack. Any idea? – Aadil Ali May 26 '16 at 08:59