这是我目前用来插入图片的代码,然而,我的网页视图在插入后失去了焦点。但是最奇怪的是,我也为keyboardWillShowNotification设置了一个通知;这个通知在插入之后被调用,但是没有键盘。
在这个问题上,有人能给出解决方案吗?
//allow me to programmatically bring up keyboard
_changWeiBo.keyboardDisplayRequiresUserAction = NO;
[_changWeiBo stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').focus()"];
[_changWeiBo stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('insertImage', false, '%@')", imagePath]];
//disallow me to programmatically bring up keyboard
_changWeiBo.keyboardDisplayRequiresUserAction = YES;发布于 2013-06-28 11:15:44
感谢@alex-i,下面的解决方案(由我自己增强)完美地解决了用户无法在插入符号位置插入图像时的错误,因为当其他视图时,UIWebview失去了焦点。
这是在插入符号位置插入图像的最小工作版本。
this.prepareInsertImage = function(){
try{
backuprange();
}
catch(exc){
log('prepareInsertImage: ' + exc);
}
}
this.insertImage = function(imgPath){
try{
restorerange();
document.execCommand("InsertImage",true,imgPath);
}
catch(exc){
log('insertImage: ' + exc);
}
}
var focusRange;
function backuprange(){
var selection = window.getSelection();
focusRange = selection.getRangeAt(0);
focusRange.setEnd(focusRange.startContainer, focusRange.startOffset);
}
function restorerange(){
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(focusRange);
}对于将来想要使用此代码的任何人,只需调用以下代码:
//Before UIImagePicker pops up
[yourWebView stringByEvaluatingJavaScriptFromString:@"prepareInsertImage()"];
//After UIImagePicker selects a image and you edit and store the image elsewhere
NSString* imagePath = @"the path for your image....";
[yourWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"insertImage('%@')",imagePath]];发布于 2013-05-27 15:04:02
我没有使用'insertImage'命令,因为我需要图像的特定格式(在我的例子中,html将保持拇指,当点击时将以全屏打开)。它应该也可以与insertImage命令一起正常工作。以下是objective-c代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSAssert([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage], @"Unhandled type: %@", [info objectForKey:UIImagePickerControllerMediaType]);
// this is just to gather some paths for the picked image
[_rtDelegate richEditor:self saveImage:[info objectForKey:UIImagePickerControllerEditedImage] completion:^(NSString *thumbPath, NSString *largePath, CGSize thumbSize) {
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"docState.insertImage('%@', '%@', %d)", thumbPath, largePath, (int)thumbSize.height/2]];
}];
[picker dismissModalViewControllerAnimated:YES];
}
-(void)onTapInsertPhoto:(UIBarButtonItem *)sender{
[self stringByEvaluatingJavaScriptFromString:@"docState.prepareInsertImage();"];
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
imagePicker.allowsEditing = YES;
UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[vc presentViewController:imagePicker animated:YES completion:nil];
[imagePicker release];
}下面是javascript (最重要的是focusRange和backupRange):
this.prepareInsertImage = function(){
try{
backuprange();
}
catch(exc){
log('prepareInertImage: ' + exc);
}
}
this.insertImage = function(thumbPath, largePath, displayHeight){
try{
restorerange();
// may work with the insertImage command here
var imgwrap = document.createElement('a');
imgwrap.href = largePath;
imgwrap.setAttribute('href', 'rte:image:'+largePath);
imgwrap.setAttribute('contenteditable', 'false');
imgwrap.className = 'imgwrap';
var img = document.createElement('img');
img.setAttribute('src', thumbPath);
img.setAttribute('height', displayHeight);
imgwrap.appendChild(img);
window.getSelection().getRangeAt(0).insertNode(imgwrap);
}
catch(exc){
log('insertImage: ' + exc);
}
}
var focusRange;
function backuprange(){
var selection = window.getSelection();
focusRange = selection.getRangeAt(0);
focusRange.setEnd(focusRange.startContainer, focusRange.startOffset);
}
function restorerange(){
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(focusRange);
}你不需要在objective-c代码中使用'docState‘,在javascript代码中使用'this’-我之所以使用它们,是因为我在javascript中有一个拥有更多方法的类,它也包含一些状态。
另请注意,keyboardDisplayRequiresUserAction仅适用于iOS 6.0+,因此以前的iOS系统不会自动显示键盘(但仍会在正确的位置插入图像)。
https://stackoverflow.com/questions/16766316
复制相似问题