3

I want the text inside the textfield to be highlighted when I tap on it.

I want the original text to be deleted the moment someone tap on the numberpad. I tried using clearButtonMode but then since my textfield size is very small the cross icon fully occupy the textfield.

Any idea how to achieve this?

Abhinav
  • 37,684
  • 43
  • 191
  • 309

5 Answers5

6

This can be achieved by

(void)textFieldDidBeginEditing:(UITextField *)iTextField {
    [iTextField selectAll:self];
}
Abhinav
  • 37,684
  • 43
  • 191
  • 309
1

The easiest way to highlight the text on tapping of the text field is simply to subclass UITextField, override becomeFirstResponder and select all text in there.

Craig Miller
  • 543
  • 8
  • 9
1

If Select All: doesn't always work here's a fix:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
}
Matjan
  • 3,591
  • 1
  • 33
  • 31
1

You need to do the highlight yourself. You could try:

  • changing the font of the text field (larger, bolder, different color)
  • overlaying a transparent UIView on top of the text field.
  • changing the background of the text field
  • change the border style

there are many options...

EDIT: in response to your question, in order to clear the field of the previous value whenever editing begins in your text field, you set up your object to conform to the UITextFieldDelegate protocol, and implement this method:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.text = nil;
}
Bogatyr
  • 19,255
  • 7
  • 59
  • 72
  • I want the original text to be deleted the moment someone tap on the numberpad. I tried using clearButtonMode but then since my textfield size is very small the cross icon fully occupy the textfield. – Abhinav Feb 13 '11 at 19:11
  • Thank you. But I do not want to clear the text field when it is in edit mode. For that there is a easy property "clearsOnBeginEditing". I want my text to be highlighted so that if someone taps on any digit in numberpad, it do two things: 1) Remove the highlighted text and 2) put the new text. However, if someone wants to take no action and get rid of the numberpad, he can still retain the old value. – Abhinav Feb 13 '11 at 19:20
0

If you still want to be able to use other delegate functions in you ViewController, I recommend you to add this:

override weak var delegate: UITextFieldDelegate? {
    didSet {
        if delegate?.isKindOfClass(YourTextField) == false {
            // Checks so YourTextField (self) doesn't set the textFieldDelegate when assigning self.delegate = self 
            textFieldDelegate = delegate
            delegate = self
        }
    }
}

// This delegate will actually be your public delegate to the view controller which will be called in your overwritten functions
private weak var textFieldDelegate: UITextFieldDelegate?

class YourTextField: UITextField, UITextFieldDelegate {

    init(){
        super.init(frame: CGRectZero)
        self.delegate = self
    }

    func textFieldDidBeginEditing(textField: UITextField) {
        textField.performSelector(Selector("selectAll:"), withObject: textField)
        textFieldDelegate?.textFieldDidBeginEditing?(textField)
    }
}

This way your view controller doesn't need to know that you have overwritted the delegate and you can implement UITextFieldDelegate functions in your view controller.

let yourTextField = YourTextField()
yourTextField.delegate = self
Jonas
  • 1,112
  • 5
  • 17
  • 28