Android/iOS键盘:贴纸API

Android/iOS keyboard: stickers API

我正在寻找一种创建自定义键盘以添加自定义表情符号和贴纸的方法。 到目前为止,我已经发现表情符号只是 unicode 字符,为了添加自定义表情符号,应该提供一种自定义字体,并将专用表情符号映射到字体内的预定义 unicode 字符。此字体应适用于应用的所有用户,以便自定义表情符号能够正确显示。

当涉及到贴纸时,问题就出现了(那些可以添加到 Facebook 评论中的大图像)。我找不到任何关于它们如何工作的有用信息,以及将它们嵌入自定义键盘并进一步粘贴到文本的界面是什么。 Google Play 和 AppStore 上有可用的应用程序(例如 "Go Keyboard" 应用程序) 谁能指出我正确的方向?

所以问题是:

如何将贴纸嵌入到文本中以进一步分享到 3rd 方应用程序?我的主要想法是了解贴纸是否有通用标准或 API(如表情符号)?或者使用贴纸的唯一方法是 use/build 与后端服务器的自定义聊天 API 这意味着只有使用相同服务的应用才能可靠地解码共享文本以正确显示贴纸?

我一直在一个 iOS 项目中工作,以证明在聊天对话中使用表情符号和贴纸的概念。

你可以在我的GitHub repository中查看,如果你愿意,可以投稿(欢迎评论和改进)。

我所做的是,使用 NSTextAttachmentUITextView 中附加图像,使用 NSAttributedString 对象类型。

要在 UITextView 中将图像显示为表情符号:

// initialize object with the content of textView
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithAttributedString:textview.attributedText];

// initialize selected image to be used as emoji
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"MicheyMouse"];
textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:25 orientation:UIImageOrientationUp];

NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributeString appendAttributedString:attrStringWithImage];

// blank space after the image
NSAttributedString *blank = [[NSAttributedString alloc] initWithString:@" "];
[attributeString appendAttributedString:blank];

textview.attributedText = attributeString;

如果您想将图像用作贴纸,请按照以下行操作:

NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:sticker];
textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:12 orientation:UIImageOrientationUp]; // --> change de scale, to change image size (or create the image in size that you want)

NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];

cell.textLabel.attributedText = attrStringWithImage

在此示例中,我将图像作为贴纸直接附加到单元格中(您可以将此单元格用作聊天气球)。

换句话说,在第一行代码中,我基本上是在 UITextView 中显示图像,而在第二行中,我将图像直接放在聊天行中。

我必须自己做 sticker/emoji 键盘,我还做了一些工作来处理表情符号键盘和打字键盘之间的切换。

这是项目示例的 GitHub 存储库:https://github.com/cairano/CIStickerFacilities