Skip to content

Instantly share code, notes, and snippets.

@jsbain
Created October 14, 2018 10:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbain/2ad26773f2ceaf3b1c3350fde62fe505 to your computer and use it in GitHub Desktop.
Save jsbain/2ad26773f2ceaf3b1c3350fde62fe505 to your computer and use it in GitHub Desktop.
inputAccessoryExample.py
# 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