views:

1122

answers:

2

I have a custom UILabel subclass for displaying currency values. I only want the user to be able to enter digits and let the view format those digits into a currency value -- like a cash register. This makes UITextField an inappropriate choice for this kind of input.

I've already overridden hitTest: so that the UILabel will return itself -- this is apparently a bug. Also, I've overridden both canBecomeFirstResponder and becomeFirstResponder to return YES. Neither of these methods are being called, though.

I only want to let the user type numbers and use the backspace key. I've implemented UITextInputTraits, but the keyboard does not appear. So, can this be done? And does if so, what am I missing?

+1  A: 

I've done something like this. Subclass UITextField, make its text color clear, and stick a label on top of it to display the formatted version.

edit - actually, strictly speaking you shouldn't even need to subclass it. Just implement the delegate method (-textField:shouldChangeCharactersInRange:replacementString:), add the label to the UITextField, and format the label's text however you need to.

Noah Witherspoon
One question: can the insertion point be hidden?
Alex
+1  A: 

OK, I've answered my own question: a UILabel placed on top of the UITextView will also hide the insertion point, but can still become the first responder and get input.

Thanks!

Alex