5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS8AppExtentionKeyboardで全ての文字を削除する

Posted at

iOS8から搭載されたAppExtentionでキーボード拡張を開発するときのTIPSです。

キーボードがフォーカスしているテキストビューの文字をすべて削除する場合

    for (int i=0; i<self.textDocumentProxy.documentContextBeforeInput.length+1; i++) {
        [self.textDocumentProxy deleteBackward];
    }

で消すのは間違いです。
何故ならばdeleteBackwardの実行には若干の時間が必要だからです。
例えば100文字の文字を上記コードで削除したあと、文字をinsertするコードを直後に書くと中途半端に削除された部分にinsertすることになります。
delayを設けても良いですが、self.textDocumentProxyにはhasTextというプロパティがあり、インプットに文字が存在するか確認することが出来ます。

    while ([self.textDocumentProxy hasText]) {
        [self.textDocumentProxy deleteBackward];
    }

これで全ての文字を削除出来ます。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?