###やりたいこと
textViewにおいてボタンをタップしたらカーソルの左の文字を1字削除することです。
###やったこと
ボタンをタップしたらカーソルが1つ左に戻ることは下記のコードで成功しました。
@IBAction func arrow03(_ sender: Any) { // To one position to the left of the current cursor position // only if there is a currently selected range if let selectedRange = InputView.selectedTextRange { // and only if the new position is valid if let newPosition = InputView.position(from: selectedRange.start, offset: -1) { // set the new position InputView.selectedTextRange = InputView.textRange(from: newPosition, to: newPosition) print("ここまで来た05") } } }
そこで下記のコードで試しましたところ、後述のエラーがでて行き詰まりました。
@IBAction func Sakujo01(_ sender: Any) { // only if there is a currently selected range if let selectedRange = InputView.selectedTextRange { // and only if the new position is valid if let newPosition = InputView.position(from: selectedRange.start, offset: -1) { // ←削除ボタンが押された時の処理 InputView.selectedText = ""
出たエラー
Value of type 'UITextView' has no member 'selectedText'
###お願い
上記のエラーを解決してボタンをタップしたらカーソルの左の文字を1字削除する方法を教えてください。
###ご参考事項
- 環境は下記のとおりです。
MacBook Pro (15-inch, 2016)
High Sierra OS10.13
Version 9.1 (9B55)Swift4にバージョンアップしました。
- 下記のように定義しています。
@IBOutlet weak var InputView: UITextView!
@IBOutlet weak var Sentaku01: UIButton!
- 画面は下記のとおりです。最初はキーボードはでておらず画面をタップして文字記入後削除ボタンを押して文字を消したいと思っております。
以上よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/03 05:58
2017/11/03 07:08 編集
2017/11/03 07:09
2017/11/03 07:24
2017/11/03 07:27
2017/11/03 08:04
2017/11/03 08:08
2017/11/03 08:14
2017/11/03 08:42
2017/11/04 00:28
2017/11/04 02:58
2017/11/04 03:55 編集
2017/11/05 02:09
2017/11/05 03:02
2017/11/05 04:33
2017/11/05 07:53
2017/11/06 00:53
2017/11/06 01:34
2017/11/08 03:06
2017/11/16 07:52
2017/11/16 11:08
2017/11/16 11:10