SHARE
TWEET

I heart Swift test keyboard

a guest Jul 22nd, 2014 538 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  KeyboardViewController.swift
  3. //  TestLeopard
  4. //
  5.  
  6. import UIKit
  7.  
  8. class KeyboardViewController: UIInputViewController {
  9.  
  10.     var nextKeyboardButton: UIButton!
  11.     var dotButton: UIButton!
  12.     var dashButton: UIButton!
  13.     var deleteButton: UIButton!
  14.     var hideKeyboardButton: UIButton!
  15.  
  16.     override func updateViewConstraints() {
  17.         super.updateViewConstraints()
  18.    
  19.         // Add custom view sizing constraints here
  20.     }
  21.  
  22.     override func viewDidLoad() {
  23.         super.viewDidLoad()
  24.    
  25.         // Perform custom UI setup here
  26.         addKeyboardButtons()
  27.     }
  28.  
  29.     override func didReceiveMemoryWarning() {
  30.         super.didReceiveMemoryWarning()
  31.         // Dispose of any resources that can be recreated
  32.     }
  33.  
  34.     override func textWillChange(textInput: UITextInput) {
  35.         // The app is about to change the document's contents. Perform any preparation here.
  36.     }
  37.  
  38.     override func textDidChange(textInput: UITextInput) {
  39.         // The app has just changed the document's contents, the document context has been updated.
  40.    
  41.         var textColor: UIColor
  42.         var proxy = self.textDocumentProxy as UITextDocumentProxy
  43.         if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
  44.             textColor = UIColor.whiteColor()
  45.         } else {
  46.             textColor = UIColor.blackColor()
  47.         }
  48.         self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
  49.     }
  50.    
  51.     func addNextKeyboardbutton() {
  52.         self.nextKeyboardButton = UIButton.buttonWithType(.System) as UIButton
  53.        
  54.         self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), forState: .Normal)
  55.         self.nextKeyboardButton.sizeToFit()
  56.         self.nextKeyboardButton.setTranslatesAutoresizingMaskIntoConstraints(false)
  57.        
  58.         self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
  59.        
  60.         self.view.addSubview(self.nextKeyboardButton)
  61.        
  62.         var nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
  63.         var nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
  64.         self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
  65.     }
  66.    
  67.     func addKeyboardButtons() {
  68.         addNextKeyboardbutton()
  69.         addDot()
  70.         addDash()
  71.         addDelete()
  72.         addHideKeyboardButton()
  73.     }
  74.    
  75.     func addDot() {
  76.         // initialize the button
  77.         dotButton = UIButton.buttonWithType(.System) as UIButton
  78.         dotButton.setTitle("·", forState: .Normal)
  79.         dotButton.sizeToFit()
  80.         dotButton.setTranslatesAutoresizingMaskIntoConstraints(false)
  81.        
  82.         // adding a callback
  83.         dotButton.addTarget(self, action: "didTapDot", forControlEvents: .TouchUpInside)
  84.        
  85.         // make the font bigger
  86.         dotButton.titleLabel.font = UIFont.systemFontOfSize(32)
  87.        
  88.         // add rounded corners
  89.         dotButton.backgroundColor = UIColor(white: 0.9, alpha: 1)
  90.         dotButton.layer.cornerRadius = 5
  91.        
  92.         view.addSubview(dotButton)
  93.        
  94.         // makes the vertical centers equa;
  95.         var dotCenterYConstraint = NSLayoutConstraint(item: dotButton, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0)
  96.        
  97.         // set the button 50 points to the left (-) of the horizontal center
  98.         var dotCenterXConstraint = NSLayoutConstraint(item: dotButton, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: -50)
  99.        
  100.         view.addConstraints([dotCenterXConstraint, dotCenterYConstraint])
  101.     }
  102.    
  103.     func didTapDot() {
  104.         var proxy = textDocumentProxy as UITextDocumentProxy
  105.        
  106.         proxy.insertText("·")
  107.     }
  108.    
  109.     func addDash() {
  110.         // initialize the button
  111.         dashButton = UIButton.buttonWithType(.System) as UIButton
  112.         dashButton.setTitle("—", forState: .Normal)
  113.         dashButton.sizeToFit()
  114.         dashButton.setTranslatesAutoresizingMaskIntoConstraints(false)
  115.        
  116.         // adding a callback
  117.         dashButton.addTarget(self, action: "didTapDash", forControlEvents: .TouchUpInside)
  118.        
  119.         // make the font bigger
  120.         dashButton.titleLabel.font = UIFont.systemFontOfSize(32)
  121.        
  122.         // add rounded corners
  123.         dashButton.backgroundColor = UIColor(white: 0.9, alpha: 1)
  124.         dashButton.layer.cornerRadius = 5
  125.        
  126.         view.addSubview(dashButton)
  127.        
  128.         // makes the vertical centers equa;
  129.         var dashCenterYConstraint = NSLayoutConstraint(item: dashButton, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0)
  130.        
  131.         // set the button 50 points to the left (-) of the horizontal center
  132.         var dashCenterXConstraint = NSLayoutConstraint(item: dashButton, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 50)
  133.        
  134.         view.addConstraints([dashCenterXConstraint, dashCenterYConstraint])
  135.     }
  136.    
  137.     func didTapDash() {
  138.         var proxy = textDocumentProxy as UITextDocumentProxy
  139.        
  140.         proxy.insertText("—")
  141.     }
  142.    
  143.     func addDelete() {
  144.         deleteButton = UIButton.buttonWithType(.System) as UIButton
  145.         deleteButton.setTitle(" Delete ", forState: .Normal)
  146.         deleteButton.sizeToFit()
  147.         deleteButton.setTranslatesAutoresizingMaskIntoConstraints(false)
  148.         deleteButton.addTarget(self, action: "didTapDelete", forControlEvents: .TouchUpInside)
  149.        
  150.         deleteButton.backgroundColor = UIColor(white: 0.9, alpha: 1)
  151.         deleteButton.layer.cornerRadius = 5
  152.        
  153.         view.addSubview(deleteButton)
  154.        
  155.         var rightSideConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: -10.0)
  156.        
  157.         var topConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: +10.0)
  158.        
  159.         view.addConstraints([rightSideConstraint, topConstraint])
  160.     }
  161.    
  162.     func didTapDelete() {
  163.         var proxy = textDocumentProxy as UITextDocumentProxy
  164.        
  165.         proxy.deleteBackward()
  166.     }
  167.    
  168.     func addHideKeyboardButton() {
  169.         hideKeyboardButton = UIButton.buttonWithType(.System) as UIButton
  170.        
  171.         hideKeyboardButton.setTitle("Hide Leopard", forState: .Normal)
  172.         hideKeyboardButton.sizeToFit()
  173.         hideKeyboardButton.setTranslatesAutoresizingMaskIntoConstraints(false)
  174.        
  175.         hideKeyboardButton.addTarget(self, action: "dismissKeyboard", forControlEvents: .TouchUpInside)
  176.        
  177.         view.addSubview(hideKeyboardButton)
  178.        
  179.         var rightSideConstraint = NSLayoutConstraint(item: hideKeyboardButton, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: -10.0)
  180.        
  181.         var bottomConstraint = NSLayoutConstraint(item: hideKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: -10.0)
  182.        
  183.         view.addConstraints([rightSideConstraint, bottomConstraint])
  184.     }
  185.  
  186. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top