Created
October 14, 2018 10:01
-
-
Save jsbain/2ad26773f2ceaf3b1c3350fde62fe505 to your computer and use it in GitHub Desktop.
inputAccessoryExample.py
This file contains hidden or 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
# coding: utf-8 | |
'''Creates a custom input accessory view, and attaches it to textfield and textview | |
this example demonstrates custom text buttons (emoji, etc), and custom actions | |
''' | |
from objc_util import * | |
import ui | |
'''set up top level view and textfield''' | |
v=ui.View(frame=[0,0,300,300],bg_color=(.92, .92, .92)) | |
tf=ui.TextField(frame=[10,10,200,30]) | |
v.add_subview(tf) | |
tv=ui.TextView(frame=[10,50,200,100]) | |
v.add_subview(tv) | |
def typeChar(sender): | |
'''finds active textinput, and types the button's title''' | |
tf=sender.objc_instance.firstResponder() | |
tf.insertText_(sender.title) | |
#create normal keys | |
buttons=[] | |
for button_title in 'πππππ³π': | |
buttons.append(ui.Button(title=button_title,action=typeChar)) | |
#create special keys | |
def prev(sender): | |
'''simulates 'tab' key, go to next field ''' | |
s=sender.objc_instance.firstResponder()._previousKeyResponder().becomeFirstResponder() | |
buttons.append(ui.Button(image=ui.Image.named('iob:ios7_arrow_back_32'),action=prev)) | |
def next(sender): | |
'''simulates 'tab' key, go to next field ''' | |
s=sender.objc_instance.firstResponder()._nextKeyResponder().becomeFirstResponder() | |
buttons.append(ui.Button(image=ui.Image.named('iob:ios7_arrow_forward_32'),action=next)) | |
'''set up toolbar''' | |
keyboardToolbar=ObjCClass('UIToolbar').alloc().init() | |
keyboardToolbar.sizeToFit() | |
keyboardToolbar.items = [ObjCClass('UIBarButtonItem').alloc().initWithCustomView_(b) for b in buttons] | |
#attach our accessory to the textfield, and textview. | |
tf.objc_instance.textField().setInputAccessoryView_(keyboardToolbar, argtypes=[c_void_p], restype=None) | |
tv.objc_instance.setInputAccessoryView_(keyboardToolbar) | |
v.present('sheet') | |
tf.begin_editing() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment