Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I select a specific block of text in a UITextField?

Tags:

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

like image 886
Sunian314 Avatar asked Jul 18 '10 22:07

Sunian314


People also ask

How do you select text in Swift?

Swift's Text views represent static, unselectable text by default, but you can change that using the . textSelection() modifier with the . enabled value.

What is a text field on Iphone?

A text field is a UI element that enables the app to get user input.


1 Answers

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


Update for iOS5 and above :

Now UITextField and UITextView conform to UITextInput protocol so it is possible :)

Selecting the last 5 characters before the caret would be 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]; 
like image 106
nacho4d Avatar answered Oct 07 '22 12:10

nacho4d