2014-09-25 4 views
4

쓰려고합니다. Custom Keyboard Extension입니다.CustomKeyBoardExtension의 현재 텍스트 선택

것은 내가 커서가 UITextField, UITextView ... 등 에서 CustomKeyboardExtension에 위치를 알 수있는 방법을 찾고 있어요 ...하지만 난 그런 아무것도 표시되지 않습니다.

SwiftKey 앱 (http://swiftkey.com)이이를 수행 할 수 있음 (또는 이와 비슷한 작업을 수행 할 수 있음)을 확인했습니다. 커서를 변경하면 제안 텍스트가 변경됩니다 (아래 그림 참조).

Q : 어떻게 현재 텍스트를 선택할 수 있습니까?

은 ...

는 UPDATE : 29/09/2014

좋아, 난 너무 바보입니다. 우리는 documentContextBeforeInput, documentContextAfterInput 메서드 textDocumentProxy 속성을 사용할 수 있습니다. 나는 "Before", "After"가 시간에 관한 것이라고 생각했다. 실제로 그것은 위치에 관한 것입니다.

죄송합니다. 나는 :(

+2

'textDocumentProxy.documentContextBeforeInput' –

+3

'documentContextBeforeInput'과 'documentContextAfterInput'중 하나만 잘라내 지 않습니다. 사용자가 자동 ​​텍스트 인 텍스트를 열면 두 가지 모두 비어 있지만 필드에는 분명히 텍스트가 있습니다. – Bersaelor

답변

1

그런 다음

-(NSString *) lastWordBeforeInput{ 
    NSArray *arrayOfSplitsString = [self.textDocumentProxy.documentContextBeforeInput componentsSeparatedByString:@" "]; 
    int countIndex = arrayOfSplitsString.count - 1; 
    NSCharacterSet *ChSet = [NSCharacterSet alphanumericCharacterSet]; 
    NSCharacterSet *invertedChSet = [ChSet invertedSet]; 
    while (countIndex > 0) { 
     NSString *lastWordOfSentance = [arrayOfSplitsString objectAtIndex:countIndex--]; 
     if ([[lastWordOfSentance stringByTrimmingCharactersInSet:invertedChSet] rangeOfCharacterFromSet:ChSet].location != NSNotFound) { 
      return [lastWordOfSentance stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     } 
    } 

    return @""; 
} 

... lastWordBeforeInput 방법을 만들기 요구 사항에 따라 textWillChange/textDidChange로 전화를 당신의 시간을 낭비.

- (void)textWillChange:(id<UITextInput>)textInput { 
    // The app is about to change the document's contents. Perform any preparation here. 
    NSLog(@"%@",[self lastWordBeforeInput]); 
} 

희망이 당신을 도울 것입니다.

+1

"Q : 현재 텍스트 선택을 어떻게받을 수 있습니까?"라는 질문에는 대답하지 않습니다. – floatingpoint