iOS 8自定义键盘

我试图build立一个自定义的键盘,就像一个表情符号键盘,但键盘的数据是从一个JSON文件。 parsing这个json文件并获取数据后,如何使自定义键盘使用它并显示在键盘视图中,如内置的表情符号键盘? 现在,我遵循应用程序扩展键盘:自定义键盘指南,这里只有less量的信息。 有没有关于如何在线创build自定义表情符号键盘的教程或指南? 目前我正在尝试的代码如下:

class KeyboardViewController: UIInputViewController { override func viewDidLoad() { super.viewDidLoad() var error: NSError? let yanFile = NSBundle.mainBundle().pathForResource("yan", ofType: "json") let yanData = NSData(contentsOfFile: yanFile) as NSData let yanDict = NSJSONSerialization.JSONObjectWithData(yanData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary println("dict: \(yanDict)") //print nothing in console // Perform custom UI setup here self.nextKeyboardButton = UIButton.buttonWithType(.System) as UIButton self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), forState: .Normal) } } 

json如下所示:

 { "list": [ { "tag": "laugh", "yan": [ "o(*≧▽≦)ツ┏━┓", "(/≥▽≤/)", "ヾ(o◕∀◕)ノ" ] }, { "tag": "wanna", "yan": [ "✪ω✪", "╰(*°▽°*)╯", "≖‿≖✧", ">ㅂ<", "ˋ▽ˊ", "✪ε✪", "✪υ✪", "ヾ (o ° ω ° O ) ノ゙", "(。◕ˇ∀ˇ◕)", "(¯﹃¯)" ] } ] } 

你可以通过点击新build文件 – > view来build立一个xib文件

1)在xib文件中创build一个uiview 320×216,你可以拖放任何你想要的控件

2)然后你可以像这样加载你的键盘到你的键盘的inputView:

 // Perform custom UI setup here UIView *layout = [[[NSBundle mainBundle] loadNibNamed:@"keyboardXib" owner:self options:nil] objectAtIndex:0]; [self.inputView addSubview:layout]; 

3)我认为,如果你build立一个JSON到键盘api,你发送一个JSON的键盘映射到你的应用程序,并且该应用程序知道如何在inputView上安排键

让我们知道,如果你build立这个项目!

编辑:

4)你需要做的大部分工作是parsingJSON,并显示你想从JSON的依赖内容,并决定他们插入到文本字段中的文本

看看这个问题: 如何parsing一个JSON文件在迅速?

祝你好运!

首先,您需要在您的KeyboardViewController中为键盘创build用户界面。 这取决于你如何定制它,添加button,视图,手势等。(顺便说一下,视图的高度是有限的,以标准的键盘高度大小,所以不要试图让它更高,它不会画)模板这是生成它只是示例显示如何可以把一个单一的button。 在你设置你的用户界面后,确保你有下一个键盘button,这是必需的。

关于表情符号 ,它不是真实的图像,它们只是unicode字符,后来被系统replace为图像。 所以你不能传递图像,你可以提供的唯一的input是NSString [self.textDocumentProxy insertText:@"hello "]; // Inserts the string "hello " at the insertion point [self.textDocumentProxy insertText:@"hello "]; // Inserts the string "hello " at the insertion point

更多细节可以在这里findhttps://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/Keyboard.html 。