ios - 检测即将发生的文本选择

标签 ios textselection

在 iOS 中,有没有一种方法可以编写当系统即将在 UIWebView 上显示文本选择控件时调用的代码?

我需要在用户想要选择某些东西时调用我的一些代码,但如果可能的话,在选择实际添加到页面之前。

最佳答案

我找到了一种使用延迟方法调用来解决这个问题的方法。选择控件不会立即出现,而是在您点击并按住某些文本大约半秒钟后才会出现。

这是我用来执行此操作的代码的粗略概述:

//in the view controller header declare a boolean
   BOOL _confirmUserIsSelectingText;

//in onTouchBegan
   _confirmUserIsSelectingText = YES;
   [self performSelector:@selector(textSelectionWillAppear:) withObject:nil afterDelay:0.3f];

//in onTouchMoved
   _confirmUserIsSelectingText = NO;

//in onTouchEnded
   _confirmUserIsSelectingText = NO;

//then define textSelectionWillAppear:    
- (void)textSelectionWillAppear:(id)ignoreMe
{
  //do whatever it is you need to happen before the selection controls appear
}

不是最好的解决方案,但它适用于我的情况。如果 textSelectionWillAppear: 需要更多时间运行,则可以调整延迟,但我认为延迟不应太接近 0.5f,否则在选择框出现之前实际上可能不会调用该方法。

关于ios - 检测即将发生的文本选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7756377/

相关文章:

html - 如何使 HTML 文本不可选择

android - 在不禁用 Web View 的超链接和按钮的情况下删除默认选择

ios - CAGradientLayer 自动旋转

ios - 在 iOS 蓝牙外设上查找 TxPower

ios - 使用AVAssetReader读取mp4文件时,第一个音频CMSampleBuffer丢失

ios - 在 NSString stringWithFormat 上崩溃

ios - 如何在编译时检查当前项目中是否存在 Swift 模块?

jquery - 使用 jQuery 设置文本选择颜色。演示不工作

css - 使用 body::selection,我想自定义高亮颜色

javascript - 防止双击选择文本