Select text inside a text field

I want the text inside the text box to be highlighted when I click on it.

I want the source text to be deleted the moment someone clicks on the number pad. I tried to use clearButtonMode, but after the size of my text field is very small, the cross icon completely occupies the text field.

Any idea how to achieve this?

+3
source share
5 answers

It can be achieved

(void)textFieldDidBeginEditing:(UITextField *)iTextField {
    [iTextField selectAll:self];
}
+6
source

You need to do the same. You can try:

  • change the font of the text field (larger, bolder, different color)
  • overlay a transparent UIView over the text box.
  • changing the background of the text box
  • change frame style

...

EDIT: , , , UITextFieldDelegate :

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.text = nil;
}
+1

- UITextField, startFirstResponder .

+1

" ": :

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
}
0

ViewController, :

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)
    }
}

, , , UITextFieldDelegate .

let yourTextField = YourTextField()
yourTextField.delegate = self
0

Source: https://habr.com/ru/post/1791862/


All Articles