我想从 UITextview 获取光标位置,我已经实现了如何在 UITextView 中找到光标的像素位置中的代码?post,该算法基于UITextView 中光标的像素位置中的算法。
我的问题是,第二次出现键盘后我无法获取 selectedRange.location 的值,因为它总是告诉位置的值是 2147483647,这意味着找不到。这是我的实现:
-(void)getCursorPosition{
NSRange originalPosition = self.codeTextView.selectedRange;
NSLog(@"original position : %d", self.codeTextView.selectedRange.location);
CGPoint origin = self.codeTextView.frame.origin;
unichar c = ' ';
NSLog(@"location : %d", self.codeTextView.selectedRange.location);
NSLog(@"length : %d", self.codeTextView.selectedRange.length);
if (self.codeTextView.selectedRange.location != [self.codeTextView.text length]) {
// NSLog(@"cek 1");
c = [self.codeTextView.text characterAtIndex:self.codeTextView.selectedRange.location];
// NSLog(@"cek 2");
}
NSLog(@"c : %d", c);
if (c!=32 && c!=10) {
NSRange delimiter = [self.codeTextView.text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
options:NSLiteralSearch
range:NSMakeRange(self.codeTextView.selectedRange.location,
[self.codeTextView.text length] - self.codeTextView.selectedRange.location)];
if (delimiter.location == NSNotFound) {
delimiter.location = [self.codeTextView.text length];
}
self.codeTextView.selectedRange = delimiter;
NSLog(@"delimiter length : %d, location : %d", delimiter.length, delimiter.location);
}
int deviationLocation = self.codeTextView.selectedRange.location - originalPosition.location;
NSString *head = [self.codeTextView.text substringToIndex:self.codeTextView.selectedRange.location];
CGSize initialSize = [head sizeWithFont:self.codeTextView.font constrainedToSize:CGSizeMake(self.codeTextView.frame.size.width,
self.codeTextView.frame.size.height)];
NSUInteger startOfLine = [head length];
BOOL isFirstLine = NO;
NSLog(@"initialize height : %f", initialSize.height);
NSLog(@"code height : %f", self.codeTextView.contentSize.height);
if (initialSize.height / self.codeTextView.contentSize.height == 1) {
isFirstLine = YES;
}
while (startOfLine > 0 && isFirstLine == NO) {
NSRange delimiter = [head rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
options:NSBackwardsSearch range:NSMakeRange(0, startOfLine)];
startOfLine = delimiter.location;
NSString *tempHead = [head substringToIndex:startOfLine];
CGSize tempHeadSize = [tempHead sizeWithFont:self.codeTextView.font constrainedToSize:CGSizeMake(self.codeTextView.frame.size.width,
self.codeTextView.frame.size.width)];
int beforeLine = initialSize.height / self.codeTextView.contentSize.height;
int afterLine = tempHeadSize.height / self.codeTextView.contentSize.height;
if (beforeLine != afterLine)
NSLog(@"break");
break;
}
NSString *tail;
if (isFirstLine == NO) {
tail = [head substringFromIndex:(startOfLine + deviationLocation)];
}else {
tail = [head substringToIndex:startOfLine - deviationLocation];
}
CGSize lineSize = [tail sizeWithFont:self.codeTextView.font forWidth:self.codeTextView.frame.size.width lineBreakMode:UILineBreakModeWordWrap];
cursor = origin;
cursor.x += lineSize.width;
cursor.y += initialSize.height - lineSize.height;
self.codeTextView.selectedRange = originalPosition;
[self.codeTextView becomeFirstResponder];
NSLog(@"cursor x : %f, y : %f", cursor.x, cursor.y);
}
我在 textViewShouldBeginEditing 方法中调用了该方法,这是实现:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[self getCursorPosition];
self.viewTextViewScroll.contentSize = CGSizeMake(self.codeTextView.frame.size.width, self.codeTextView.contentSize.height+100);
CGRect frameCode = self.codeTextView.frame;
frameCode.size.height = self.codeTextView.contentSize.height + 103;
self.codeTextView.frame = frameCode;
CGRect frameLine = self.lineNumberTextView.frame;
frameLine.size.height = self.codeTextView.contentSize.height +100;
self.lineNumberTextView.frame = frameLine;
return YES;
}
有人可以帮我吗
更新 :
我已经解决了这个问题,使用 textViewDidChangeSelection 委托方法,这是我所做的代码:
-(void)textViewDidChangeSelection:(UITextView *)textView{
counterAppear++;
if (counterAppear == 1) {
}
else if (counterAppear == 2) {
isFistAppear = NO;
codeBuilderSelectedRange = textView.selectedRange;
[self getCursorPosition];
CGFloat xPos = cursor.x - (0.5*self.view.frame.size.width);
if (cursor.x < self.view.frame.size.width) {
[[[self.viewCodeTextView subviews] lastObject] setContentOffset:CGPointMake(0, cursor.y) animated:YES];
}else {
[[[self.viewCodeTextView subviews] lastObject] setContentOffset:CGPointMake(xPos, cursor.y) animated:YES];
}
}else if (isFistAppear == NO) { //kondisi saat keyboard masih appear & user pindah2 kursor
codeBuilderSelectedRange = textView.selectedRange;
[self getCursorPosition];
NSLog(@"cursor x :%f, y : %f", cursor.x, cursor.y);
}
}
我在 viewDidLoad 中设置了 BOOL isFirstAppear,我设置了这个,因为在我 nslog textViewDidChangeSelection 中的 selectedRange.location 值之后,它总是调用两次,第一个值总是给出一个未找到的值,但第二个给出一个正确的值,所以我设置像那样。我在键盘方法中设置的 counterAppear(我在 viewDidLoad 中创建了一个由 NSNotification 调用的方法)。counterAppear 是当我点击 textview 时给我差异条件的值。