iOS 8 自定义键盘

标签 ios keyboard swift ios8 ios-app-extension

我正在尝试构建一个自定义键盘,它就像一个表情符号键盘,但键盘的数据来自一个 json 文件。解析这个json文件并获取数据后,如何让自定义键盘使用它并在键盘 View 中显示,就像内置的表情符号键盘一样?现在,我遵循 App Extension Keyboard: Custom Keyboard 指南,这里只有少量信息。是否有任何关于如何在线创建自定义表情符号键盘的教程或指南?我正在尝试的当前代码如下:

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 ) ノ゙",
                "(。◕ˇ∀ˇ◕)",
                "(¯﹃¯)"
            ]
        }
    ]
}

最佳答案

您可以通过点击新建文件->查看来构建一个xib文件

1) 在 xib 文件中创建一个 uiview 320x216,你可以将任何你想要的控件拖放到其中

2) 然后你可以像这样将 Nib 加载到键盘的输入 View 中:

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

3) 我认为如果你构建一个 JSON 到键盘 api 会很棒 您将键盘映射的 JSON 发送到您的应用程序 并且应用程序知道如何相应地在 inputView 上排列按键

如果您构建此项目,请告诉我们!

编辑:

4) 您需要做的大部分工作是解析 JSON 并从 JSON uibuttons 中显示您想要的内容,并决定将什么文本插入到文本字段中

检查这个问题:How to parse a JSON file in swift?

祝你好运!

关于iOS 8 自定义键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24340061/

相关文章:

ios - 如何从gmail联系人中获取 friend 的个人资料图片?

keyboard - UWP 虚拟键盘将内容向上推

ios - 将 unwind segue 与 UISearchController 一起使用时出错

swift - 如何在 Swift 中将字符串转换为 Int?

iOS [AppDelegate TerminateWithSuccess] : unrecognized selector sent to instance

ios - Storyboard中的奇怪行为(运行应用程序时缺少 View )

ios - 如何将 CIImage 保存到 Photos 并保持其 P3 色彩空间完好无损?

xcode - 带小数点的数学应用程序崩溃

javascript - 浏览器中不可见元素的键盘快捷键

ios - 跟踪应用程序的运行时间 iOS