Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
KeyboardViewController.swift source from AppDesignVault for Swift 3
//
// KeyboardViewController.swift
// Demo Custom Keyboard
//
// Created by Drew Lustro on 10/20/14.
// Updated by Jared Bigcraft on 10/29/2016 to work with Swift 3
//
// Original derived from tutorial found on AppDesignVault
// http://www.appdesignvault.com/ios-8-custom-keyboard-extension/
//
//
// *** ALL AUTOLAYOUT CONSTRAINTS HAVE BEEN FIXED! ;) NO MORE WARNINGS
//
// https://gist.github.com/drewlustro/641d5844e43e87d0c011
// https://gist.github.com/critical186/4c05e2700fd233507ef1460bd8d969f7
//
import UIKit
class KeyboardViewController: UIInputViewController {
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
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 = ["CP", "Z", "X", "C", "V", "B", "N", "M", "BP"]
let buttonTitles4 = ["CHG", "SPACE", "RETURN"]
let row1 = createRowOfButtons(buttonTitles: buttonTitles1 as [NSString])
let row2 = createRowOfButtons(buttonTitles: buttonTitles2 as [NSString])
let row3 = createRowOfButtons(buttonTitles: buttonTitles3 as [NSString])
let row4 = createRowOfButtons(buttonTitles: buttonTitles4 as [NSString])
self.view.addSubview(row1)
self.view.addSubview(row2)
self.view.addSubview(row3)
self.view.addSubview(row4)
row1.translatesAutoresizingMaskIntoConstraints = false
row2.translatesAutoresizingMaskIntoConstraints = false
row3.translatesAutoresizingMaskIntoConstraints = false
row4.translatesAutoresizingMaskIntoConstraints = false
addConstraintsToInputView(inputView: self.view, rowViews: [row1, row2, row3, row4])
}
func createRowOfButtons(buttonTitles: [NSString]) -> UIView {
var buttons = [UIButton]()
let keyboardRowView = UIView(frame: CGRect(x:0, y:0, width:320, height:50))
// keyboardRowView.tag = self.rowCount++
for buttonTitle in buttonTitles {
let button = createButtonWithTitle(title: buttonTitle as String)
buttons.append(button)
keyboardRowView.addSubview(button)
}
addIndividualButtonConstraints(buttons: buttons, rowView: keyboardRowView)
return keyboardRowView
}
func createButtonWithTitle(title: String) -> UIButton {
let button = UIButton.init(type: .system) as UIButton
button.frame = CGRect(x:0, y:0, width:20, height:20)
button.setTitle(title, for: .normal)
button.sizeToFit()
button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(white: 1.0, alpha: 1.0)
button.setTitleColor(UIColor.darkGray, for: .normal)
button.addTarget(self, action:#selector(didTapButton(_:)), for: .touchUpInside)
return button
}
func didTapButton(_ sender: AnyObject?) {
let button = sender as! UIButton
let proxy = textDocumentProxy as UITextDocumentProxy
print("Pressed:\(button.title(for: .normal) as String?)")
if let title = button.title(for: .normal) as String? {
switch title {
case "BP":
proxy.deleteBackward()
case "RETURN":
proxy.insertText("\n")
case "SPACE":
proxy.insertText(" ")
case "CHG":
self.advanceToNextInputMode()
default:
//print("default Title:\(title)")
proxy.insertText(title)
}
}
}
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
//let proxy = self.textDocumentProxy as UITextDocumentProxy
//if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
// textColor = UIColor.white
//} else {
// textColor = UIColor.black
//}
}
func addIndividualButtonConstraints(buttons: [UIButton], rowView: UIView){
for (index, button) in buttons.enumerated() {
let topConstraint = NSLayoutConstraint(item: button, attribute: .top, relatedBy: .lessThanOrEqual, toItem: rowView, attribute: .top, multiplier: 1.0, constant: 1.0)
let bottomConstraint = NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .greaterThanOrEqual, toItem: rowView, attribute: .bottom, multiplier: 1.0, constant: -1.0)
var rightConstraint : NSLayoutConstraint!
if index == buttons.count - 1 {
rightConstraint = NSLayoutConstraint(item: button, attribute: .right, relatedBy: .greaterThanOrEqual, toItem: rowView, attribute: .right, multiplier: 1.0, constant: 0.0)
} else {
let nextButton = buttons[index+1]
rightConstraint = NSLayoutConstraint(item: button, attribute: .right, relatedBy: .equal, toItem: nextButton, attribute: .left, multiplier: 1.0, constant: -1.0)
}
var leftConstraint : NSLayoutConstraint!
if index == 0 {
leftConstraint = NSLayoutConstraint(item: button, attribute: .left, relatedBy: .lessThanOrEqual, toItem: rowView, attribute: .left, multiplier: 1.0, constant: 0.0)
} else {
let prevtButton = buttons[index-1]
leftConstraint = NSLayoutConstraint(item: button, attribute: .left, relatedBy: .equal, toItem: prevtButton, attribute: .right, multiplier: 1.0, constant: 1.0)
let firstButton = buttons[0]
let widthConstraint = NSLayoutConstraint(item: firstButton, attribute: .width, relatedBy: .equal, toItem: button, attribute: .width, multiplier: 1.0, constant: 0.0)
widthConstraint.priority = 800.0
rowView.addConstraint(widthConstraint)
}
rowView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
}
}
func addConstraintsToInputView(inputView: UIView, rowViews: [UIView]){
for (index, rowView) in rowViews.enumerated() {
let rightSideConstraint = NSLayoutConstraint(item: rowView, attribute: .right, relatedBy: .equal, toItem: inputView, attribute: .right, multiplier: 1.0, constant: 0.0)
let leftConstraint = NSLayoutConstraint(item: rowView, attribute: .left, relatedBy: .equal, toItem: inputView, attribute: .left, multiplier: 1.0, constant: 0.0)
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.0)
} else {
let prevRow = rowViews[index-1]
topConstraint = NSLayoutConstraint(item: rowView, attribute: .top, relatedBy: .equal, toItem: prevRow, attribute: .bottom, multiplier: 1.0, constant: 0.0)
let firstRow = rowViews[0]
let heightConstraint = NSLayoutConstraint(item: firstRow, attribute: .height, relatedBy: .equal, toItem: rowView, attribute: .height, multiplier: 1.0, constant: 0.0)
heightConstraint.priority = 800.0
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.0)
} else {
let nextRow = rowViews[index+1]
bottomConstraint = NSLayoutConstraint(item: rowView, attribute: .bottom, relatedBy: .equal, toItem: nextRow, attribute: .top, multiplier: 1.0, constant: 0.0)
}
inputView.addConstraint(bottomConstraint)
}
}
}
@critical186

This comment has been minimized.

Copy link
Owner Author

@critical186 critical186 commented Oct 30, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment