IOS 8键盘如何在十进制和数字键盘之间切换+ shift键

时间:2014-12-06 08:14:00

标签: ios keyboard decimal numeric shift

我正忙着制作IOS 8键盘,我需要一些帮助。

我已经制作了一个基本的键盘,一切正常,但我有一个颠簸,我需要一些帮助来解决它们。我在swift中编写了代码(请记住,我是swift的新手)。

我需要以下方面的帮助:

  1. 要将键盘从十进制(正常的字母)转换为数字(数字/符号一),我为它编写了一些代码(它基本上分别隐藏了键盘并再次显示它们)但我不是&# 39;当我点击" 123"时,我知道如何让它运行按钮。

  2. 当我键入一个键时,例如。 " A"它打印出它在键盘上的确切显示方式如何自动将其设为小写,然后实现一个移位按钮给我大写字母?

  3. 非常感谢一些帮助解决这个问题。

    这是我到目前为止的代码:

    import UIKit
    
    class KeyboardViewController: UIInputViewController
    {
    
    @IBOutlet var nextKeyboardButton: UIButton!
    
    var allButtons = [UIButton]()
    
    override func updateViewConstraints()
    {
        super.updateViewConstraints()
    
        // Add custom view sizing constraints here
    }
    
    override func viewDidLoad()
    {
    
        //Normal Keyboard start
        super.viewDidLoad()
    
        let buttonTitles1 = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"]
        let buttonTitles2 = ["A", "S", "D", "F", "G", "H", "J", "K", "L"]
        let buttonTitles3 = ["⬆︎", "Z", "X", "C", "V", "B", "N", "M", "⬅︎"]
        let buttonTitles4 = ["123","","⎯", "Return"]
    
        //⚒
    
        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])
    
        applyCurrentTheme()
        //Normal keyboard end
    
    
    
    
    
    
    
        //Change to numeric keyboard start
    
        let buttonsNum1FirstRow = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
        let buttonsNum2FirstRow = ["-", "/", ":", ";", "(", ")", "$", "&", "@", "\""]
        let buttonsNum3FirstRow = ["#+=", ".", ",", "?", "!", "'", "7", "⬅︎"]
        let buttonsNum4FirstRow = ["ABC","⎯","Return"]
    
        //define rows as class variable instead of function variable
    
        var Srow1:UIView!
        var Srow2:UIView!
        var Srow3:UIView!
        var Srow4:UIView!
        var Srow1Num:UIView!
    
        //Hide it like below:
        func didTapChangeToNumbersButton(sender: AnyObject?)
        {
            Srow1.hidden = true
    
            Srow1Num.hidden = false
    
        }
        //Change to numeric keyboard end
    
    
    }
    
    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)
        }
    
        allButtons += buttons
        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(10)
        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
    
    
        let capon = false;
        let capslast = false;
        let capslock = false;
        let spacelast = false;
    
    
    
        if let title = button.titleForState(.Normal)
        {
    
    
            switch title
            {
    
            case "" :
                self.advanceToNextInputMode()
    
    
            case "." :
                let capon = true
                proxy.insertText(".")
                let spacelast = false
                let capslast = false
    
         //Caps idea
         //this caps lock code is experimental and actually doesn't work, just an idea I was playing with
            case "⬆︎" :
                let capon = !capon
                if capslock
                {
                    let capon = true
                    let capslock = false
                    let capslast = false
                }
    
                let capslast = true
           //Caps idea
    
            case "⬅︎" :
                proxy.deleteBackward()
                let spacelast = false
                let capslast = false
    
            case "Return" :
                proxy.insertText("\n")
                let spacelast = false
                let capslast = false
    
            case "⎯" :
                if spacelast
                {
                    proxy.deleteBackward()
                    proxy.insertText(". ")
                    let spacelast = false
                    let capon = true
                }
    
                else
                {
                    proxy.insertText(" ")
                    let spacelast = true
                }
                let capslast = false
    
    
    
            default :
                proxy.insertText(title)
    
    
            }
        }
    }
    
    
    
    func applyCurrentTheme()
    {
    
        var theme = ClaptonTheme()
    
        theme.applyThemeToView(self.view)
    
        theme.applyThemeToKeys(allButtons)
    }
    
    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)
        }
    
    }
    }
    

0 个答案:

没有答案