UIWebView with contentEditable (html editing), first responder handling?(UIWebView 与 contentEditable(html 编辑),第一响应者处理?)
问题描述
我正在为一个应用程序制作一个 html 编辑器组件(在 iOS 5.0 中使用带有 contentEditable 的 UIWebView),并陷入了如何处理 UIWebView 第一响应者状态
I'm making an html editor component for an app (using UIWebView with contentEditable in iOS 5.0), and got stuck at how to handle UIWebView first responder status
[webView isFirstResponder]、[webView becomeFirstResponder] 和 [webView resignFirstResponder] 似乎不起作用,我不知道如何通过代码使 webView 成为或退出它
[webView isFirstResponder], [webView becomeFirstResponder] and [webView resignFirstResponder] don't seem to work, and i've no idea how to make the webView become or resign it by code
如果有人知道如何解决这个问题,我将非常感激,在此先感谢!
If anyone knows how to work this out i would be very grateful, thanks in advance!
推荐答案
下面是我如何在 UIWebView
子类中覆盖这些方法(content
是可编辑的 id元素):
Here is how I overwrite these methods in a UIWebView
subclass (content
is the id of the editable element):
-(BOOL)resignFirstResponder {
[self setUserInteractionEnabled:NO];[self setUserInteractionEnabled:YES];
return [super resignFirstResponder];
}
// only works on iOS 6+
-(void)becomeFirstResponder {
self.keyboardDisplayRequiresUserAction = NO; // set here or during initialization
// important note: in some situations (newer iOS versions), it is also required to first call `blur()` on the 'content' element, otherwise the keyboard won't show up as expected
[self stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').focus()"];
}
-(BOOL)isFirstResponder{
if ([[self stringByEvaluatingJavaScriptFromString:@"document.activeElement.id=='content'"] isEqualToString:@"true"]) {
return YES;
}
else {
return NO;
}
}
isFirstResponder
只会在键盘显示后返回 true(例如,在 UIKeyboardWillShowNotification 会返回 false)
isFirstResponder
will only return true after the keyboard is shown (e.g, it will return false at UIKeyboardWillShowNotification)
如果这是一个问题,另一种检查 UIWebView
是否是第一响应者的方法如下:
In case this is an issue, another way to check if the UIWebView
is the first responder is as follows:
+(BOOL)isFirstResponder:(UIView *)v{
for (UIView *vs in v.subviews) {
if ([vs isFirstResponder] || [self isFirstResponder:vs]) {
return YES;
}
}
return NO;
}
-(BOOL)isFirstResponder{
return [[self class] isFirstResponder:self];
}
这样,即使在键盘动画完成(显示或隐藏)之前/之后,返回的值也会是YES
.
This way, the returned value will be YES
even before/after the keyboard animation finishes (showing or hiding).
这篇关于UIWebView 与 contentEditable(html 编辑),第一响应者处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIWebView 与 contentEditable(html 编辑),第一响应者处理?
基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01