UITextPosition到Int [英] UITextPosition to Int
本文介绍了UITextPosition到Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个UISearchBar,我试图设置一个光标位置。我正在使用UITectField委托,因为我找不到任何直接用于UISearchBar的东西。以下是我正在使用的代码:
I have a UISearchBar on which I am trying to set a cursor position. I am using UITectField delegates as I couldn't find anything direct for UISearchBar. Below is the code I am using:
UITextField *textField = [searchBar valueForKey: @"_searchField"];
// Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];
// Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:selectedRange.start toPosition:selectedRange.end];
问题 位于'newRange'中'toPosition'的对象我希望有类似selectedRange.end-1的东西;因为我希望光标位于倒数第二位。
Question is in the 'newRange' object for 'toPosition' I want to have something like selectedRange.end-1; as I want the cursor to be on second last position.
如何将光标设置到倒数第二位?
How do I set the cursor to second last position?
推荐答案
线索是建立一个位置,然后是一个没有长度的范围,然后选择它
the clue is to make a position and then a range with no length and then select it
例如
- (IBAction)select:(id)sender {
//get position: go from end 1 to the left
UITextPosition *pos = [_textField positionFromPosition:_textField.endOfDocument
inDirection:UITextLayoutDirectionLeft
offset:1];
//make a 0 length range at position
UITextRange *newRange = [_textField textRangeFromPosition:pos
toPosition:pos];
//select it to move cursor
_textField.selectedTextRange = newRange;
}
这篇关于UITextPosition到Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文