UITextView获取前一个字符 - (即光标位置前的字符)(iOS7)

时间:2014-06-18 12:07:44

标签: ios7 uitextview uitextposition uitextrange

我无法弄清楚如何获得输入的前一个字符。换句话说,如何在光标之前的任何位置获取UITextView内的字符。

NSInteger pos = [self.textInput offsetFromPosition:self.textInput.beginningOfDocument toPosition:textrange.start];

CGRect s = [self.textInput caretRectForPosition:self.textInput.selectedTextRange.start];

UITextRange *textrange = [self.textInput characterRangeAtPoint: s.origin];

谢谢,

亲切的问候,

威尔

1 个答案:

答案 0 :(得分:3)

谢谢它是我正在寻找的,我确实在实际看到之前,但并非100%确定,我稍微修改了它的好。

    UITextPosition *beginning = self.textInput.beginningOfDocument;
    UITextPosition *selectionStart = self.textInput.selectedTextRange.start;
    UITextPosition *selectionEnd = self.textInput.selectedTextRange.end;
    NSInteger location = [self.textInput offsetFromPosition:beginning toPosition:selectionStart];
    NSInteger length = [self.textInput offsetFromPosition:selectionStart toPosition:selectionEnd];
    NSRange selectedRange = NSMakeRange(location,length);
    NSRange range = NSMakeRange(selectedRange.location-1,1);

    NSString *lastChar = [textValue substringWithRange:range];