我可以在UITextField中select特定的文本块吗?
我的iPhone应用程序中有一个UITextField。 我知道如何使文本字段select所有的文本,但如何改变select? 假设我想select最后的5个字符,或者一个特定的字符范围,这是可能的吗? 如果不是,我可以移动标记select开始和结束的行,就好像用户正在拖动它们一样?
用UITextField,你不能。 但是,如果您看到这些标题,则可以使用_selectedRange和其他可能用于添加某些类别的标题;)
iOS5及更高版本的更新:
现在UITextField和UITextView符合UITextInput协议,所以有可能:)
插入前的最后5个字符就是这样的:
// 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]; 
要select一个特定的字符范围,你可以在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; 
 由于UITextField和其他UIKit元素都有它们自己的UITextPosition和UITextRange的私有子类, UITextPosition不能直接创build新的值,但是可以使用文本字段从引用到文本的开始或结尾以及整数抵消。 
你也可以做相反的操作来得到当前select的开始点和结束点的整数表示:
 int start = [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.start]; int end = [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.end]; 
 这是一个添加方法来处理使用NSRangeselect的类别。  https://gist.github.com/4463233 
这些都为我工作:
 [UITextField selectAll:self]; 
和:
 UITextField.selectedRange = NSMakeRange(0, 5); 
但只有在文本字段是第一响应者的情况下。 (换句话说,只有当键盘显示时。)所以,你需要在这两个select方法之前:
 [UITextField becomeFirstResponder]; 
如果你想让文字显示被选中。
要select没有文件扩展名的文件的名称,请使用以下命令:
 -(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]; } 
只是接受答案的一小部分。 在iOS 5中,当UITextView的文本长度为0(没有文本)时,以下行会崩溃我的应用程序:
 [self textRangeFromPosition:startPosition toPosition:endPosition]; 
 一个小的解决方法是添加一个length == 0检查,如: 
 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]; } 
迅速
select最后5个字符:
 if let newPosition = textField.positionFromPosition(textField.endOfDocument, inDirection: UITextLayoutDirection.Left, offset: 5) { textField.selectedTextRange = textField.textRangeFromPosition(newPosition, toPosition: textField.endOfDocument) } 
select一个任意范围:
 // 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!) } 
我的完整答案在这里 。