NSString 从 UITextView 中光标的位置搜索字符

NSString search for character from the location of the cursor in a UITextView

我一直在尝试弄清楚如何找到指定范围(即光标的当前位置)中出现的字符。

我知道如何使用

找到光标的当前位置
NSRange cursorPosition = [textView.text selectedRange];

但我想弄清楚如何从 cursorPosition 向后搜索。

我想做的是,例如,如果我有一个字符串:

NSString *string = @"Hello I am tagging @xyz to notify them of the tag"

假设在string中,光标的位置正好在"to"之前,我想从光标所在的位置开始搜索到字符串中@的位置,并且取 cursorPosition@.

的子串

如果我的描述含糊或写得不好,请告诉我,我会进一步解释。

任何帮助都会很棒!非常感谢!

编辑:非常感谢您的宝贵时间和回复!

只需使用rangeOfString:options:range:.

// Get the current selection range
NSRange cursorPosition = [textView.text selectedRange];
if (cursorPosition.location != NSNotFound) {
    // Build range from start of text up to the start of the selection
    NSRange searchRange = NSRangeMake(0, cursorPosition.location);
    // Find the desired substring within the range
    NSRange matchRange = [textView.text rangeOfString:@"@" options:NSBackwardsSearch range:searchRange];
    if (matchRange.location != NSNotFound) {
        // Build range starting with the found substring up to the start of the selection
        NSRange textRange = NSRangeMake(matchRange.location, searchRange.length - matchRange.location);
        // Get the text in the desired range
        NSString *matchingText = [textView.text substringInRange:textRange];
        NSLog(@"Matching text to caret is %@", matchingText);
    } else {
        NSLog(@"No match up to the caret");
    }
} else {
    NSLog(@"No selection");
}

请注意,此代码查找文本视图中任何当前选择的开头。如果要在当前选择范围内搜索,则需要进行相应调整。

它对我有用。 放置光标并进入后退按钮进行测试

-(BOOL)textView:(nonnull UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(nonnull NSString *)text{

NSRange rangeFrom = [textView.text rangeOfString:@"@"];
NSString *result = [textView.text substringWithRange:NSMakeRange(rangeFrom.location+1, range.location-rangeFrom.location)];
NSLog(@"result:%@",result);//result:xyz


return YES;
}

// .........************ 检查这个....为什么这不起作用....*/

NSRange cursorPosition = NSMakeRange(23, 1);//[textView.text range];//selectedRange
NSString *string = @"Hello I am tagging @xyz to notify them of the tag";
NSRange rangeFrom = [string rangeOfString:@"@"];
NSString *result = [string substringWithRange:NSMakeRange(rangeFrom.location+1, cursorPosition.location-rangeFrom.location)];
NSLog(@"result:%@",result);//result:xyz

试试这个

NSRange cursorPosition = [textView.text selectedRange];
NSString *string = @"Hello I am tagging @xyz to notify them of the tag";
    NSInteger loc = [string rangeOfString : @"@"].location;
    NSRange range = NSMakeRange(loc, (cursorPosition.location - loc));
    NSString *newString = [string substringWithRange: range];
NSInteger cursorPosition = [_textView selectedRange].location;

NSInteger stringPosition = [_textView.text rangeOfString:@"@"].location;

if (stringPosition < cursorPosition) {
    stringPosition = stringPosition + 1;
}

NSInteger lengthFinal = labs(cursorPosition - stringPosition);
NSRange range = NSMakeRange(MIN(cursorPosition, stringPosition), lengthFinal);

NSString *finalSubString = [_textView.text substringWithRange:range];