如何以编程方式将页面添加到我的自定义键盘以及类别(类似于Apple表情符号键盘)?

时间:2014-10-28 15:32:39

标签: ios xcode swift keyboard ios8

如何以编程方式将页面添加到自定义键盘和类别(类似于Apple表情符号键盘)?

如何以编程方式在Swift中添加页面(向右滑动以获取更多表情符号 - 在本例中为文本)到我的自定义键盘,如Apple的表情符号键盘?

我还想知道苹果如何为他们的表情符号创建他们的自定义类别,比如花和它们?我假设他们使用数组,但是我如何在Swift中以编程方式进行呢?

import UIKit

class KeyboardViewController: UIInputViewController {

    @IBOutlet var nextKeyboardButton: UIButton!

    override func updateViewConstraints() {
        super.updateViewConstraints()

        // Add custom view sizing constraints here
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let buttonTitles1 = ["TEXT0"]
        let buttonTitles2 = ["TEXT1"]
        let buttonTitles3 = ["TEXT2"]
        let buttonTitles4 = ["", "", "", "", "", "", "Aa", "⌫"]

        var row1 = createRowOfButtons(buttonTitles1)
        var row2 = createRowOfButtons(buttonTitles2)
        var row3 = createRowOfButtons(buttonTitles3)
        var row4 = createRowOfButtons(buttonTitles4)

        self.view.addSubview(row1)
        self.view.addSubview(row2)
        self.view.addSubview(row3)
        self.view.addSubview(row4)

        row1.setTranslatesAutoresizingMaskIntoConstraints(false)
        row2.setTranslatesAutoresizingMaskIntoConstraints(false)
        row3.setTranslatesAutoresizingMaskIntoConstraints(false)
        row4.setTranslatesAutoresizingMaskIntoConstraints(false)

        addConstraintsToInputView(self.view, rowViews: [row1, row2, row3, row4])
    }

    func createRowOfButtons(buttonTitles: [NSString]) -> UIView {

        var buttons = [UIButton]()
        var keyboardRowView = UIView(frame: CGRectMake(0, 0, 320, 50))

        for buttonTitle in buttonTitles{

            let button = createButtonWithTitle(buttonTitle)
            buttons.append(button)
            keyboardRowView.addSubview(button)
        }

        addIndividualButtonConstraints(buttons, mainView: keyboardRowView)

        return keyboardRowView
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated
    }

    override func textWillChange(textInput: UITextInput) {
        // The app is about to change the document's contents. Perform any preparation here.
    }

    override func textDidChange(textInput: UITextInput) {
        // The app has just changed the document's contents, the document context has been updated.

        var textColor: UIColor
        var proxy = self.textDocumentProxy as UITextDocumentProxy
        if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
            textColor = UIColor.whiteColor()
        } else {
            textColor = UIColor.blackColor()
        }
    }

    func createButtonWithTitle(title: String) -> UIButton {

        let button = UIButton.buttonWithType(.System) as UIButton
        button.frame = CGRectMake(0, 0, 20, 20)
        button.setTitle(title, forState: .Normal)
        button.sizeToFit()
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTranslatesAutoresizingMaskIntoConstraints(false)
        button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)

        button.addTarget(self, action: "didTapButton:", forControlEvents: .TouchUpInside)

        return button
    }

    func didTapButton(sender: AnyObject?) {

        let button = sender as UIButton
        var proxy = textDocumentProxy as UITextDocumentProxy

        if let title = button.titleForState(.Normal) {
            switch title {
            case "TEXT1" :
                proxy.insertText("hi")
            case "ENTER" :
                proxy.insertText("\n")
            case "TEXT 2" :
                proxy.insertText("YOLO")
            case "" :
                self.advanceToNextInputMode()
            case "TEXT0" :
                proxy.insertText("hello")

            case "⌫" :
                proxy.deleteBackward()
            default :
                proxy.insertText("WUT?!")
            }
        }
    }

    func addIndividualButtonConstraints(buttons: [UIButton], mainView: UIView){

        for (index, button) in enumerate(buttons) {

            var topConstraint = NSLayoutConstraint(item: button, attribute: .Top, relatedBy: .Equal, toItem: mainView, attribute: .Top, multiplier: 1.0, constant: 1)

            var bottomConstraint = NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: mainView, attribute: .Bottom, multiplier: 1.0, constant: -1)

            var rightConstraint : NSLayoutConstraint!

            if index == buttons.count - 1 {

                rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: mainView, attribute: .Right, multiplier: 1.0, constant: -1)

            }else{

                let nextButton = buttons[index+1]
                rightConstraint = NSLayoutConstraint(item: button, attribute: .Right, relatedBy: .Equal, toItem: nextButton, attribute: .Left, multiplier: 1.0, constant: -1)
            }

            var leftConstraint : NSLayoutConstraint!

            if index == 0 {

                leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: mainView, attribute: .Left, multiplier: 1.0, constant: 1)

            }else{

                let prevtButton = buttons[index-1]
                leftConstraint = NSLayoutConstraint(item: button, attribute: .Left, relatedBy: .Equal, toItem: prevtButton, attribute: .Right, multiplier: 1.0, constant: 1)

                let firstButton = buttons[0]
                var widthConstraint = NSLayoutConstraint(item: firstButton, attribute: .Width, relatedBy: .Equal, toItem: button, attribute: .Width, multiplier: 1.0, constant: 0)

                widthConstraint.priority = 800
                mainView.addConstraint(widthConstraint)
            }

            mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
        }
    }

    func addConstraintsToInputView(inputView: UIView, rowViews: [UIView]){

        for (index, rowView) in enumerate(rowViews) {
            var rightSideConstraint = NSLayoutConstraint(item: rowView, attribute: .Right, relatedBy: .Equal, toItem: inputView, attribute: .Right, multiplier: 1.0, constant: -1)

            var leftConstraint = NSLayoutConstraint(item: rowView, attribute: .Left, relatedBy: .Equal, toItem: inputView, attribute: .Left, multiplier: 1.0, constant: 1)

            inputView.addConstraints([leftConstraint, rightSideConstraint])

            var topConstraint: NSLayoutConstraint

            if index == 0 {
                topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: inputView, attribute: .Top, multiplier: 1.0, constant: 0)

            }else{

                let prevRow = rowViews[index-1]
                topConstraint = NSLayoutConstraint(item: rowView, attribute: .Top, relatedBy: .Equal, toItem: prevRow, attribute: .Bottom, multiplier: 1.0, constant: 0)

                let firstRow = rowViews[0]
                var heightConstraint = NSLayoutConstraint(item: firstRow, attribute: .Height, relatedBy: .Equal, toItem: rowView, attribute: .Height, multiplier: 1.0, constant: 0)

                heightConstraint.priority = 800
                inputView.addConstraint(heightConstraint)
            }
            inputView.addConstraint(topConstraint)

            var bottomConstraint: NSLayoutConstraint

            if index == rowViews.count - 1 {
                bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: inputView, attribute: .Bottom, multiplier: 1.0, constant: 0)

            }else{

                let nextRow = rowViews[index+1]
                bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .Bottom, relatedBy: .Equal, toItem: nextRow, attribute: .Top, multiplier: 1.0, constant: 0)
            }

            inputView.addConstraint(bottomConstraint)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我会尽量避免这种方法。我试着像你一样做,因为页面控制元素(由Apple制作的表情符号键盘上的点来切换页面)似乎不适用于我的自定义键盘。我创建了一个xib文件并将该元素拖到xib文件上,该文件托管了我的所有按钮(当然这可以通过编程方式完成),但不是在我的模拟器上显示它使我的键盘消失。我以编程方式实现了页面控制元素,并得到了相同的结果。这次我有不同的做法:

  • 创建一个用于切换当前按钮的按钮,或一个用于修改键盘上先前按钮的值和图像的按钮。

我发现这种方式更容易。我自己没有实现它,但我相信现在是在自定义键盘上切换按钮的良好开端。希望有人会回答有关如何合并页面控制元素的问题。

希望这会有所帮助。