2012-12-13 3 views
2

uiwebview에로드 된 내용 편집 가능한 html을 사용하고 있습니다. 키보드가 숨겨 지거나 표시 될 때 커서 위치를 설정하는 코드가 필요합니다.uiwebview에로드 된 내용을 편집 가능한 html로 사용하고 있습니다. 키보드가 숨겨 지거나 표시 될 때 커서 위치를 설정하는 코드가 필요합니다.

현재 웹보기 키보드를 클릭하면 키보드가 나타나지만 내용은 키보드 뒤에 숨겨집니다. Return 키를 계속 누르면 커서/텍스트가 webview 뒤에서 표시되거나 보이지 않습니다.

헤드 스타트의 경우 iPad Evernote 응용 프로그램에 사용 된 것과 같은 기능이 필요합니다. 커서가 키보드 뒤를 따라 가지 않는다는 것을 알게되면, 항상 키보드 위에서 커서가 시작됩니다.

답변

0

저는 이것을 위해 javascript를 사용하고 있습니다. 코드를 좀 더 체계적으로 유지하기 위해 클래스를 사용하고 있습니다 (코드에서 this을 볼 수 있습니다).하지만 그럴 필요는 없습니다. 오브젝티브 -C에서

// this is used to get the current coordinates of the selection - not very efficient, so it shouldn't be called too often 
this.updateOffset = function() { 
    try{ 
    var sel = window.getSelection(); 
    range = sel.getRangeAt(0); 
    if(this.tmpSpan==null){ 
     this.tmpSpan = document.createElement('span'); 
    } 
    range.insertNode(this.tmpSpan); 
    this.yOffset = this.tmpSpan.offsetTop; 
    this.xOffset = this.tmpSpan.offsetLeft; 
    this.tmpSpan.parentNode.removeChild(this.tmpSpan); 
    } 
    catch(exc){ 
    log('updateOffset:' + exc.toString()); 
    } 
} 

// eContent is the div with 'contenteditable', while visibleHeight is an int, set from objective-c (depending on where the webview is positioned, keyboard height and screen height) 
this.scrollToVisible = function(){ 
    try { 
    if(this.eContent.clientHeight>this.visibleHeight){ 
     this.updateOffset(); 
     if(this.yOffset<window.pageYOffset){ 
     window.scrollTo(0, this.yOffset); 
     } 
     else if(this.yOffset-window.pageYOffset>this.visibleHeight){ 
     window.scrollTo(0, this.yOffset-this.visibleHeight); 
     } 
    } 
    } 
    catch (exc){ 
    log('scrollToVisible: ', exc.toString()); 
    } 
} 

나는 보여주는 키보드 동안 visibleHeight을 설정하고 키보드가 보여주는 완료되면 이후 scrollToVisible를 호출하고 있습니다.

-(void)setVisibleHeight:(int)height{ 
    [self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"docState.visibleHeight=%d", height]]; 
} 
-(void)scrollToVisible{ 
    [self stringByEvaluatingJavaScriptFromString:@"docState.scrollToVisible()"]; 
} 

scrollToVisible는 자바 스크립트 이벤트라고 : onKeyUp에, onpaset, oncut 여러 라인 '복귀'또는 포장을 누를 때 문제를 해결한다.

이 방법을 선택하는 경우 자바 스크립트를 스크롤 할 때 매우주의해야합니다. 그렇지 않으면 UIWebview 컨트롤에 몇 가지 문제가 발생할 수 있습니다 (예 : 잘못된 위치에 커서를 놓고 커서를 자동으로 이동). 문서 등의 위)

편집
visibleHeight에 대한 일부 설명합니다. 내가 기억할 수있는 것에서는 자바 스크립트에서 실제로 보이는 높이를 얻을 수 없었기 때문에 이것을 사용했습니다 (document.body.clientHeight에는 키보드 뒤의 영역도 포함됩니다). 내가 UIWebView의 서브 클래스에서이 전화 드렸습니다

- (void)keyboardWillShow:(NSNotification *)notification { 
    ... 
    NSDictionary *userInfo = [notification userInfo]; 
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGRect keyboardRect = [aValue CGRectValue]; 
    CGRect kbRect = [self.window convertRect:keyboardRect fromView:nil]; 
    _kbRect = kbRect; 

    CGPoint sorigin = [self.superview convertPoint:self.frame.origin toView:nil]; 
    int visibleHeight = _kbRect.origin.y-sorigin.y-_tlbInputAccessory.frame.size.height-lkPadBottom; // _tlbInputAccessory is a custom accessory view 
    [self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"docState.setVisibleHeight(%d)", height]]; 
    ... 
} 

주, 그래서 self가를 나타냅니다 : 나는 전체 화면으로 UIWebView을 제시하고 있습니다 때문에 다음과 같이

, 나는 눈에 보이는 높이를 설정하고있어 UIWebView 컨트롤.

+0

호출 방법 - (void) setVisibleHeight : (int) height 나는 높이를 의미합니까? –

+0

@MoorthyTheBoss는 'visibleHeight'에 대한 자세한 정보는 편집을 참조하십시오. –

+0

lkPadBottom이라는 오브젝트가 무엇인지 알려 주실 수 있습니까? –