Custom UIView with keyboard input?

I have a custom UILabel subclass to display currency values. I want the user to be able to enter numbers and allow the forms to represent those numbers in currency - like a cash register. This makes UITextField an unacceptable choice for such input.

I've already overridden hitTest: so that the UILabel will return by itself - this appears to be an error. Also, I have overridden both canBecomeFirstResponder and startFirstResponder to return YES. However, none of these methods are called.

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

+1


source to share


2 answers


I did something like this. Subclass UITextField, clear the text color and paste over it to display the formatted version.



edit - in fact, strictly speaking, you don't even need to subclass it. Just implement the delegate method (-textField: shouldChangeCharactersInRange: replacementString :), add the label to the UITextField and format the label text, but you need to.

+1


source


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



Thank!

+1


source







All Articles