我想从UITextView的光标位置,我已经实现How to find a pixel-positon of a cursor in UITextView?后的代码,该算法在Pixel-Position of Cursor in UITextView基于算法。我如何获得selectedRange.location值?
我的问题是,后键盘出现在第2次我不能让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;
}
有人可以帮助我,请
UPDATE:
我已经解决了这个问题,用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);
}
}
我设置BOOL isFirstAppear在viewDidLoad中,我设置因为,在我的NSLog在textViewDidChangeSelection的selectedRange.location值,它总是叫了两声,第一个值总是给出一个值未找到,但是第二给出一个正确的价值,所以我设置这样的。我在键盘方法设置的counterAppear(ⅰ制成的,通过在NSNotification称为viewDidLoad中的方法)。 counterAppear是当我点击textview时给予差异条件的值。
谢谢你的回答,但我已经解决了这个问题,很遗憾我还没有更新这个问题 – 2011-03-07 04:11:07