how can i get selectedRange.location value?

本小妞迷上赌 提交于 2020-01-02 20:10:02

问题


i want to get a cursor position from UITextview, i have implement the code in How to find a pixel-positon of a cursor in UITextView? post, the algorithm is based on algorithm in Pixel-Position of Cursor in UITextView.

my problem is, i can't get the selectedRange.location value after keyboard appear in the 2nd time, cause it always tell that the value of location is 2147483647, which means not found. This is my implementation :

-(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);

}

i called that method in textViewShouldBeginEditing method, this is the implementation :

-(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;

}

can somebody help me, please

UPDATE :

i have solved this problem, with using textViewDidChangeSelection delegate method, here is the code i've made :

-(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);
}

}

i set the BOOL isFirstAppear in viewDidLoad, i set this because, after i nslog the selectedRange.location value in textViewDidChangeSelection, it always called twice, the first value always give a not found value, but the second give a right value, so i set like that. The counterAppear i set in keyboard method (i made a method that called by NSNotification in viewDidLoad). counterAppear is the value that give me the difference condition when i tap the textview.


回答1:


In textViewDidBeginEditing, the location value will wrong sometimes. To fix this, do this:

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


来源:https://stackoverflow.com/questions/4613978/how-can-i-get-selectedrange-location-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!