質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

542閲覧

textViewにおいてボタンで文字を選択したい。⇒エラーValue of type 'UITextView' has no memberの解決

Tomzy

総合スコア104

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/10/30 06:20

###やりたいこと
音声入力をアシストするアプリを制作中です。年寄りは指が乾き標準の方式でカーソルを動かしたり、文字を選択するのが難しいのでボタンを配置して音声入力で原稿をつくれるようにするアプリです。
イメージ説明
当面やりたいことは全選択ボタンをタップした時入力済みの文字を全選択するコーディングのエラーを解消したいことです。

###選択ボタンをタップするActionでエラー
カーソルを動かすActionは成功したのですが、全選択とカーソルの左側の文字を選択するコーディングを実装するとエラーがでます。参考にしたURLは
リンク内容
です。

###書いたコード

// VoiceAsisst01 // // import UIKit class ViewController: UIViewController, UITextViewDelegate { @IBOutlet weak var InputView: UITextView! @IBOutlet weak var Arrow01: UIButton! @IBOutlet weak var Arrow02: UIButton! //途中省略 @IBOutlet weak var Sentaku01: UIButton! @IBOutlet weak var Sentaku02: UIButton! @IBOutlet weak var Sentaku03: UIButton! @IBOutlet weak var Sentaku04: UIButton! //途中省略 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) InputView.text = "画面に触れるとキーボードがでます。そのマイクロフォンを押してiPhoneに向かってしゃべると文字が入力されます。削除するときなどは上のボタンを使う" InputView.textColor = UIColor.lightGray //キーボードを自動で表示 self.inputView?.becomeFirstResponder() } //カーソル01を押した時⇒一番前にカーソルが戻る⇒成功 @IBAction func arrow01(_ sender: Any) { let newPosition = InputView.beginningOfDocument InputView.selectedTextRange = InputView.textRange(from: newPosition, to: newPosition) } //カーソル02を押した時⇒一番後ろにカーソルが進む⇒成功 @IBAction func arrow02(_ sender: Any) { let newPosition = InputView.endOfDocument InputView.selectedTextRange = InputView.textRange(from: newPosition, to: newPosition) } //全選択を押した時⇒全選択する⇒エラー @IBAction func sentaku01(_ sender: Any) { InputView.selectedRange = InputView.selectedTextRangeFromPosition(InputView.beginningOfDocument, toPosition: InputView.endOfDocument) } // }) //←選択を押した時⇒左側を選択する⇒エラー @IBAction func sentaku02(_ sender: Any) { // Range: 3 to 7 let startPosition = textView.positionFromPosition(textView.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: 3) let endPosition = textView.positionFromPosition(textView.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: 7) if startPosition != nil && endPosition != nil { textView.selectedTextRange = textView.textRangeFromPosition(startPosition!, toPosition: endPosition!) } override func viewDidLoad() { super.viewDidLoad() InputView.delegate = self func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } }

###エラーのでるところ
イメージ説明

###エラー内容
Value of type 'UITextView' has no member 'selectedTextRangeFromPosition'

このエラーはカーソルを動かすところでも出ましたが、参照コードを変更したら収まりました。

###ご参考事項

  • 環境は下記のとおりです。

MacBook Pro (15-inch, 2016)
High Sierra OS10.13
Xcode 9.0 (9A235)Swift4

  • //←選択を押した時⇒左側を選択する⇒エラー

の部分のコードは参考にした Range: 3 to 7を選択するコードを入れただけです。エラーが解決してから、一文字選択に修正する予定です。

  • まずは多発する 'UITextView' has no member のエラーを解決する方法をご教示頂ければと思います。よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

下記のコードでボタンによる全テキストの選択ができるよになりました。
なお、この解決は下記のURLより情報を得ました。
https://stackoverflow.com/questions/42801919/uitextview-begin-editing-with-all-text-selected-in-uitableviewcell

//全選択を押した時⇒全選択する

@IBAction func sentaku01(_ sender: Any) { InputView.selectAll(nil) InputView.selectAll(self) func textViewDidBeginEditing(InputView: UITextView) { DispatchQueue.main.async { InputView.selectAll(nil) print("ここまで来た14") } }

}

投稿2017/11/14 06:00

Tomzy

総合スコア104

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

selectedTextRangeFromPositionではなくtextRangeFromPositionではないでしょうか?

【追記】

Swift4(おそらくSwift3も)では、textRange(from:to:)です。

投稿2017/10/30 07:02

編集2017/10/30 07:29
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Tomzy

2017/10/30 07:19

早速のご回答ありがとうございました。 //全選択を押した時⇒全選択する @IBAction func sentaku01(_ sender: Any) { InputView.selectedRange = InputView.textRangeFromPosition(InputView.beginningOfDocument, toPosition: InputView.endOfDocument) } と修正しましたが、 Value of type 'UITextView' has no member 'textRangeFromPosition' というエラーがでます。修正の仕方に誤りがありますか。再度ご教示頂ければ幸いです。
Tomzy

2017/10/30 09:40

下記のように再修正してみました。 //全選択を押した時⇒全選択する @IBAction func sentaku01(_ sender: Any) { // InputView.selectedRange = InputView.textRangeFromPosition(InputView.beginningOfDocument, toPosition: InputView.endOfDocument) InputView.selectedRange = InputView.textRange(from:InputView.beginningOfDocument, to:InputView.endOfDocument) } } しかし、Cannot assign value of type 'UITextRange?' to type 'NSRange' (aka '_NSRange') というエラーがでました。
Tomzy

2017/10/30 09:46

下記の再再修正での結果もエラーでした。 //全選択を押した時⇒全選択する @IBAction func sentaku01(_ sender: Any) { // InputView.selectedRange = InputView.textRangeFromPosition(InputView.beginningOfDocument, toPosition: InputView.endOfDocument) // InputView.selectedRange = InputView.textRange(from:InputView.beginningOfDocument, to:InputView.endOfDocument) // } //} //再再修正 InputView.selectedRange = InputView.textRangeFromPosition(from:InputView.beginningOfDocument, to:InputView.endOfDocument) } エラー表示は Value of type 'UITextView' has no member 'textRangeFromPosition'
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問