@wenchoheelio , below is another way to consider filtering your TextField. Ie using a TextField Delegate. This way you are getting a peek at the key before it makes it into the TextField(you can accept or reject it, by either returning True or False). Below I have just copied the standard Delegate from the Pythonista Documentation. I just modified the textfield_should_change callback.
I could have deleted all the other methods in the class as in this case I am not using them. They will just do their default behaviour if they are not there. I point that out because the code looks longer and more complicated that it needs to to handle your case. I left them in so it was easy to see the other events you can take control of. Basically meaning you can have a lot of control over the data entry into the TextField.
Btw, I am looping over the replacement as it maybe a string that is being pasted into the textfield. You can try it by copying say '123456' and pasting it into your field. It will appear. But then try pasting '123456R' into the field, that will not work.
Hope it helps.
import ui
class MyTextFieldDelegate (object):
def textfield_should_begin_editing(self, textfield):
return True
def textfield_did_begin_editing(self, textfield):
pass
def textfield_did_end_editing(self, textfield):
pass
def textfield_should_return(self, textfield):
textfield.end_editing()
return True
def textfield_should_change(self, textfield, range, replacement):
for c in replacement:
if not c.isdigit():
return False
return True
def textfield_did_change(self, textfield):
pass
class MyClass(ui.View):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.make_view()
def make_view(self):
tf = ui.TextField(frame=(0, 0, self.width, 32))
tf.delegate = MyTextFieldDelegate()
self.add_subview(tf)
if __name__ == '__main__':
f = (0, 0, 300, 400)
v = MyClass(frame=f)
v.present(style='sheet', animated=False)