2016-01-21 206 views
43

私はUITextFieldを実験していて、カーソル位置の操作方法を実験しています。私はSwiftのUITextFieldとUITextViewのカーソル位置の取得と設定

のように、関係のObjective-Cの回答数を見つけた。しかし、私はスウィフトと協力しておりますので、私はどのように勉強したかったです現在のカーソル位置を取得し、Swiftで設定します。

以下の答えは、私の実験とObjective-Cの翻訳の結果です。

答えて

136

この回答は以下の内容がUITextFieldUITextViewの両方に適用されスウィフト3

用に更新されました。

お役立ち情報

テキストフィールドのテキストの冒頭:

let startPosition: UITextPosition = textField.beginningOfDocument 

テキストフィールドのテキストの最後:

let endPosition: UITextPosition = textField.endOfDocument 

現在選択されている範囲:

let selectedRange: UITextRange? = textField.selectedTextRange 

、位置を設定するために、カーソル位置

if let selectedRange = textField.selectedTextRange { 

    let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start) 

    print("\(cursorPosition)") 
} 

セットカーソル位置

を取得し、これらの方法の全ては、実際には同一の開始および終了値と範囲を設定します。終始

let newPosition = textField.beginningOfDocument 
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition) 

let newPosition = textField.endOfDocument 
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition) 

現在のカーソル位置の左への1つの位置に

// only if there is a currently selected range 
if let selectedRange = textField.selectedTextRange { 

    // and only if the new position is valid 
    if let newPosition = textField.position(from: selectedRange.start, offset: -1) { 

     // set the new position 
     textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition) 
    } 
} 

任意の位置へ

先頭から始めて5文字を移動します。

関連

let arbitraryValue: Int = 5 
if let newPosition = textField.position(from: textField.beginningOfDocument, offset: arbitraryValue) { 

    textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition) 
} 

すべてのテキスト

textField.selectedTextRange = textField.textRange(from: textField.beginningOfDocument, to: textField.endOfDocument) 

を選択して、現在のカーソルpositioでテキスト

// Range: 3 to 7 
let startPosition = textField.position(from: textField.beginningOfDocument, offset: 3) 
let endPosition = textField.position(from: textField.beginningOfDocument, offset: 7) 

if startPosition != nil && endPosition != nil { 
    textField.selectedTextRange = textField.textRange(from: startPosition!, to: endPosition!) 
} 

挿入テキストの範囲を選択しますnは

textField.insertText("Hello") 

ノート

  • 使用textField.becomeFirstResponder()は、テキストフィールドにフォーカスを与え、キーボードが表示されるようにします。

  • 一部の範囲でテキストを取得する方法については、this answerを参照してください。これと他のスレッドが働いていたから、他の

    func textViewDidBeginEditing(_ textView: UITextView) { 
    
        DispatchQueue.main.async{ 
         textField.selectedTextRange = ... 
        } 
    } 
    

    何も:

は、私がDispatchQueueを使用していた私の場合も

+0

startPositionの目的は何ですか?あなたはそれで何ができますか?また、カーソル位置を取得する必要がある目的は何ですか? – Honey

+1

@ハニー、私はここで2つの異なるものを参照するためにその変数名を使用しました。最初は、テキストフィールドの先頭を参照することでした。カーソルをそこに移動したい場合は便利です。 2番目は選択された範囲のテキストを募集することでした。その範囲をコピーするか、その範囲を設定する場合に便利です。 – Suragch

+0

テキストフィールドが空のときにカーソルを右に移動する方法はありますか?私はあなたの答えに従ったが、私はそれを作ることができなかった。 –

2

参照してください。

PS:textViewDidBeginEditingが実行されていたスレッドがメインスレッドであることを確認しました。すべてのUIが実行されるので、main.asynchを使用したわずかな遅延が働いた理由がわかりません。

関連する問題