Is it possible to select a specific block of text in a UITextField?

I have a UITextField in my iPhone app. I know how to make the text box select all the text, but how to change the selection? Say I wanted to select the last 5 characters or a specific range of characters, is this possible? if not, can I move the lines indicating the beginning and end of the selection, as if the user was dragging them?

+24
textselection iphone uitextfield
Jul 18 '10 at 22:35
source share
7 answers

With UITextField you cannot. But if you see the headers, you have _selectedRange and others that can be used if you add some categories to it;)


Update for iOS5 and above:

Now UITextField and UITextView conform to the UITextInput protocol, so this is possible :)

Select the last 5 characters before the carriage looks like this:

// Get current selected range , this example assumes is an insertion point or empty selection UITextRange *selectedRange = [textField selectedTextRange]; // Calculate the new position, - for left and + for right UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:-5]; // Construct a new range using the object that adopts the UITextInput, our textfield UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start]; // Set new range [textField setSelectedTextRange:newRange]; 
+31
Jul 19 '10 at 4:31
source share

To select a specific range of characters, you can do something similar in iOS 5+

 int start = 2; int end = 5; UITextPosition *startPosition = [self positionFromPosition:self.beginningOfDocument offset:start]; UITextPosition *endPosition = [self positionFromPosition:self.beginningOfDocument offset:end]; UITextRange *selection = [self textRangeFromPosition:startPosition toPosition:endPosition]; self.selectedTextRange = selection; 

Since UITextField and other UIKit elements have their own subclasses UITextPosition and UITextRange , you cannot create new values ​​directly, but you can use a text field to create them for you from a link to the beginning or end of the text and an integer offset.

You can also do the opposite to get whole representations of the start and end points of the current selection:

 int start = [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.start]; int end = [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.end]; 

Here is a category that adds methods for handling selections using NSRange s. https://gist.github.com/4463233

+13
Jan 05 '13 at 19:00
source share

To select a file name without a file extension, use this:

 -(void) tableViewCellDidBeginEditing:(UITableViewTextFieldCell*) cell { NSInteger fileNameLengthWithoutExt = [self.filename length] - [[self.filename pathExtension] length]; UITextField* textField = cell.textField; UITextPosition* start = [textField beginningOfDocument]; UITextPosition* end = [textField positionFromPosition:start offset: fileNameLengthWithoutExt - 1]; // the -1 is for the dot separting file name and extension UITextRange* range = [textField textRangeFromPosition:start toPosition:end]; [textField setSelectedTextRange:range]; } 
+5
Dec 11 '13 at 15:12
source share

These both work for me:

 [UITextField selectAll:self]; 

and

 UITextField.selectedRange = NSMakeRange(0, 5); 

but only if the text field is the first responder. (In other words, only if the keyboard is showing.) So, you need to precede any of the select methods with this:

 [UITextField becomeFirstResponder]; 

if you want the text to be selected.

+3
Apr 27 2018-11-11T00:
source share

Just a small addition to the accepted answer. In iOS 5, the following line interrupts my application when the text length of the UITextView is 0 (no text):

 [self textRangeFromPosition:startPosition toPosition:endPosition]; 

A small workaround is to add a length == 0 check:

 if (textField.text && textField.text.length > 0) { // Get current selected range , this example assumes is an insertion point or empty selection UITextRange *selectedRange = [textField selectedTextRange]; // Calculate the new position, - for left and + for right UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:-5]; // Construct a new range using the object that adopts the UITextInput, our textfield UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start]; // Set new range [textField setSelectedTextRange:newRange]; } 
+1
Aug 31 '13 at 14:02
source share

Swift

Select the last 5 characters:

 if let newPosition = textField.positionFromPosition(textField.endOfDocument, inDirection: UITextLayoutDirection.Left, offset: 5) { textField.selectedTextRange = textField.textRangeFromPosition(newPosition, toPosition: textField.endOfDocument) } 

Choose a custom range:

 // Range: 3 to 7 let startPosition = textField.positionFromPosition(textField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: 3) let endPosition = textField.positionFromPosition(textField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: 7) if startPosition != nil && endPosition != nil { textField.selectedTextRange = textField.textRangeFromPosition(startPosition!, toPosition: endPosition!) } 

My complete answer is here .

0
Jan 21 '16 at 12:02
source share

Swift 5.0

this is how i choose the Panda file name from Panda.txt

 func textFieldDidBeginEditing(_ textField: UITextField) { // if textField.text is 'Panda.txt', offset will be 3 let offset = String(textField.text!.split(separator: ".").last!).length let from = textField.position(from: textField.beginningOfDocument, offset: 0) let to = textField.position(from: textField.beginningOfDocument, offset:textField.text!.length - (offset+1) ) //now 'Panda' will be selected textField.selectedTextRange = textField.textRange(from: from!, to: to!) } 
-one
Sep 19 '19 at 8:20
source share



All Articles