我怎样才能获得selectedRange.location值?

时间:2011-01-06 10:35:01

标签: ipad keyboard uiscrollview uitextview cursor-position

我想从UITextview获取光标位置,我在How to find a pixel-positon of a cursor in UITextView?帖子中实现了代码,该算法基于Pixel-Position of Cursor in 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时给出差异条件的值。

1 个答案:

答案 0 :(得分:1)

在textViewDidBeginEditing中,位置值有时会出错。要解决此问题,请执行以下操作:

-(void)textViewDidBeginEditing:(UITextView *)textView {
[self performSelector:@selector(beginEditing:) withObject:textView afterDelay:0]; }-(void)beginEditing:(UITextView*)sender {
//code here }