テキストフィールドをタップすると、テキストフィールド内のテキストが強調表示されます。
誰かが数字パッドをタップした瞬間に元のテキストを削除したいのですが。clearButtonModeを使用してみましたが、テキストフィールドのサイズが非常に小さいため、十字アイコンがテキストフィールドを完全に占有しています。
これを達成する方法はありますか?
テキストフィールドをタップすると、テキストフィールド内のテキストが強調表示されます。
誰かが数字パッドをタップした瞬間に元のテキストを削除したいのですが。clearButtonModeを使用してみましたが、テキストフィールドのサイズが非常に小さいため、十字アイコンがテキストフィールドを完全に占有しています。
これを達成する方法はありますか?
これは、
(void)textFieldDidBeginEditing:(UITextField *)iTextField {
[iTextField selectAll:self];
}
ハイライトは自分で行う必要があります。あなたは試すことができます:
多くのオプションがあります...
編集:質問に答えて、テキストフィールドで編集が開始されるたびに前の値のフィールドをクリアするには、UITextFieldDelegateプロトコルに準拠するようにオブジェクトを設定し、次のメソッドを実装します。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.text = nil;
}
テキストフィールドをタップしてテキストを強調表示する最も簡単な方法は、UITextFieldをサブクラス化し、becomeFirstResponderをオーバーライドして、そこにあるすべてのテキストを選択することです。
[すべて選択]が常に機能するとは限らない場合は、次の修正があります。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
}
それでも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)
}
}
このように、View Controllerは、デリゲートを上書きしたことを知る必要がなく、ViewControllerにUITextFieldDelegate関数を実装できます。
let yourTextField = YourTextField()
yourTextField.delegate = self