IOS: Text Selection in WKWebView (WKSelectionGranularityCharacter)

青春壹個敷衍的年華 提交于 2019-11-27 11:55:57

问题


I've got an app that uses a web view where text can be selected. It's long been an annoyance that you can't select text across a block boundary in UIWebView. WKWebView seems to fix this with a property on its configuration: selectionGranularity. One of the possible values is WKSelectionGranularityCharacter:

Selection endpoints can be placed at any character boundary.

Sounds great! Exactly what I need. Except that when I set that in my web view, I often can no longer select text at all. What is going on? Is there something else I need to set? Has anyone figured this out?

Update: I've figured out the following bugs:

  1. When there is more than one WKWebView in an app with selectionGranularity set to WKSelectionGranularityCharacter, only the most recent one to load can select text. I've filed this as bug 18441138.

  2. If there is a click handler attached to an element inside the body in the HTML content of a WKWebView whose selectionGranularity is set to WKSelectionGranularityCharacter, text selection doesn't work inside that element. I've filed this as bug 18440833.

  3. Text selection fails in the WKWebView after you've entered edit mode on a UITextView somewhere else in the app until the WKWebView reloads. I've filed this as bug 18468405.

Has anyone worked around any of these?


回答1:


It's a several known bugs that still is not fixed.

You can execute JavaScript to get selected text as a workaround.

window.getSelection().toString()

Docs




回答2:


These and other issues are all fixed as of iOS 13.



来源:https://stackoverflow.com/questions/25938393/ios-text-selection-in-wkwebview-wkselectiongranularitycharacter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!