3

テキストフィールドをタップすると、テキストフィールド内のテキストが強調表示されます。

誰かが数字パッドをタップした瞬間に元のテキストを削除したいのですが。clearButtonModeを使用してみましたが、テキストフィールドのサイズが非常に小さいため、十字アイコンがテキストフィールドを完全に占有しています。

これを達成する方法はありますか?

4

5 に答える 5

6

これは、

(void)textFieldDidBeginEditing:(UITextField *)iTextField {
    [iTextField selectAll:self];
}
于 2011-02-15T19:11:59.000 に答える
1

ハイライトは自分で行う必要があります。あなたは試すことができます:

  • テキストフィールドのフォントを変更する(大きく、太字で、異なる色)
  • テキストフィールドの上に透明なUIViewをオーバーレイします。
  • テキストフィールドの背景を変更する
  • ボーダースタイルを変更する

多くのオプションがあります...

編集:質問に答えて、テキストフィールドで編集が開始されるたびに前の値のフィールドをクリアするには、UITextFieldDelegateプロトコルに準拠するようにオブジェクトを設定し、次のメソッドを実装します。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.text = nil;
}
于 2011-02-13T19:04:06.533 に答える
1

テキストフィールドをタップしてテキストを強調表示する最も簡単な方法は、UITextFieldをサブクラス化し、becomeFirstResponderをオーバーライドして、そこにあるすべてのテキストを選択することです。

于 2014-03-21T11:05:38.267 に答える
1

[すべて選択]が常に機能するとは限らない場合は、次の修正があります。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField performSelector:@selector(selectAll:) withObject:textField afterDelay:0.f];
}
于 2014-03-25T18:08:35.410 に答える
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)
    }
}

このように、View Controllerは、デリゲートを上書きしたことを知る必要がなく、ViewControllerにUITextFieldDelegate関数を実装できます。

let yourTextField = YourTextField()
yourTextField.delegate = self
于 2016-02-11T12:39:04.147 に答える