Last active
January 4, 2019 04:10
-
-
Save kazuhiro4949/da5c8d5159e4dd8707f766f0661ee892 to your computer and use it in GitHub Desktop.
Show UIPickerView as "Custom Keyboard"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class PickerKeyboard: UIControl { | |
var data: [String] = ["Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sut.", "Sun."] | |
fileprivate var textStore: String = "" | |
override func draw(_ rect: CGRect) { | |
UIColor.black.set() | |
UIRectFrame(rect) | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.alignment = .center | |
let attrs: [String: AnyObject] = [NSFontAttributeName: UIFont.systemFont(ofSize: 17), NSParagraphStyleAttributeName: paragraphStyle] | |
NSString(string: textStore).draw(in: rect, withAttributes: attrs) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) | |
} | |
func didTap(sender: PickerKeyboard) { | |
becomeFirstResponder() | |
} | |
func didTapDone(sender: UIButton) { | |
resignFirstResponder() | |
} | |
override var canBecomeFirstResponder: Bool { | |
return true | |
} | |
override var inputView: UIView? { | |
let pickerView = UIPickerView() | |
pickerView.delegate = self | |
let row = data.index(of: textStore) ?? -1 | |
pickerView.selectRow(row, inComponent: 0, animated: false) | |
return pickerView | |
} | |
override var inputAccessoryView: UIView? { | |
let button = UIButton(type: .system) | |
button.setTitle("Done", for: .normal) | |
button.addTarget(self, action: #selector(PickerKeyboard.didTap(sender:)), for: .touchUpInside) | |
button.sizeToFit() | |
let view = UIView(frame: CGRect(x: 0, y: 0, width: self.bounds.width, height: 44)) | |
view.autoresizingMask = [.flexibleHeight, .flexibleWidth] | |
view.backgroundColor = .groupTableViewBackground | |
button.frame.origin.x = 16 | |
button.center.y = view.center.y | |
button.autoresizingMask = [.flexibleRightMargin, .flexibleBottomMargin, .flexibleTopMargin] | |
view.addSubview(button) | |
return view | |
} | |
} | |
extension PickerKeyboard: UIKeyInput { | |
// It is not necessary to store text in this situation. | |
var hasText: Bool { | |
return !textStore.isEmpty | |
} | |
func insertText(_ text: String) { | |
textStore += text | |
setNeedsDisplay() | |
} | |
func deleteBackward() { | |
textStore.remove(at: textStore.characters.index(before: textStore.characters.endIndex)) | |
setNeedsDisplay() | |
} | |
} | |
extension PickerKeyboard: UIPickerViewDelegate, UIPickerViewDataSource { | |
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | |
return data.count | |
} | |
func numberOfComponents(in pickerView: UIPickerView) -> Int { | |
return 1 | |
} | |
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { | |
return data[row] | |
} | |
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { | |
textStore = data[row] | |
setNeedsDisplay() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コードありがとうございます!使わせていただきます。
23行目のコードで、
PickerKeyboard.didTap(sender:)
となっていますが、
PickerKeyboard.didTapDone(sender:)
ではありませんか?