A small code to allow (the daughter of) @estephan500 and @runjaj to use a real numeric pad on iPad, with your own
- cursor color
- number of rows
- icons for backspace and enter/return/done
In your site-packages folder, create SetTextFieldAsNumeric.py with
import ui
from objc_util import *
def SetTextFieldAsNumeric(tf):
tfo = ObjCInstance(tf).textField() # UITextField is subview of ui.TextField
#print(dir(tfo))
def key_pressed(sender):
tfb = sender.TextField
tfobjc = ObjCInstance(tfb).textField()
cursor = tfobjc.offsetFromPosition_toPosition_(tfobjc.beginningOfDocument(), tfobjc.selectedTextRange().start())
if sender.name == 'x':
if cursor > 0:
#if tfb.text != '':
tfb.text = tfb.text[:cursor-1] + tfb.text[cursor:]
cursor = cursor - 1
elif sender.name == 'y':
tfb.end_editing()
return
else:
tfb.text = tfb.text[:cursor] + sender.title + tfb.text[cursor:]
cursor = cursor + 1
# set cursor
cursor_position = tfobjc.positionFromPosition_offset_(tfobjc.beginningOfDocument(), cursor)
tfobjc.selectedTextRange = tfobjc.textRangeFromPosition_toPosition_(cursor_position, cursor_position)
# design your keyboard
# | = new row
# x = back space => left delete
# y = done => discard the keyboard
keys = '123x|456|789y| 0'
#keys = '1234567890x y'
rows = keys.split('|')
row_length = 0
for row in rows:
if len(row) > row_length:
row_length = len(row)
v = ui.View()
db = 50
dd = 10
x0 = (ui.get_screen_size()[0]-row_length*db-(row_length-1)*dd)/2
x = x0
y = 0
for row in rows:
for c in row:
if c != ' ':
b = ui.Button()
b.name = c
b.background_color = 'white' # or any other color
b.tint_color = 'black'
b.corner_radius = 10
if c == 'x':
b.image = ui.Image.named('typb:Delete')
elif c == 'y':
b.title = ''
b.image = ui.Image.named('emj:Checkmark_3').with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
#b.image = ui.Image.named('iob:ios7_checkmark_outline_32')
else:
b.title = c
b.font = ('<system>',32)
b.frame = (x,y,db,db)
b.TextField = tf # store tf as key attribute needed when pressed
b.action = key_pressed
v.add_subview(b)
x = x + db + dd
y = y + db + dd
x = x0
v.width = ui.get_screen_size()[0]
v.height = y
# view of keyboard
retain_global(v) # see https://forum.omz-software.com/topic/4653/button-action-not-called-when-view-is-added-to-native-view
tfo.setInputView_(ObjCInstance(v))
# color of cursor and selected text
tfo.tintColor = UIColor.redColor().colorWithAlphaComponent(0.5)
You can use it like this:
import ui
from SetTextFieldAsNumeric import SetTextFieldAsNumeric
tf = ui.TextField()
SetTextFieldAsNumeric(tf)
tf.text = ''
tf.width = 400
tf.present('sheet')
tf.begin_editing()
tf.wait_modal()

or




