How can I turn off OS X's Smart Quotes replacement in my WKWebView?

随声附和 提交于 2020-04-16 02:34:07

问题


I'm developing a Mac application that contains a WKWebView. The HTML in the web view contains an input field into which the user types code (Ruby, in fact). When I type a quote (") into this field, it is automatically turned into a fancy curly quote (“). Since Ruby strings use ASCII quotes as delimiters this is wrong.

At the moment I'm taking what the user enters and substituting from curly quotes to ASCII quotes before using the code, but obviously this is not ideal.

I can't figure out how I can disable this OS X feature. Obviously it's possible for the user to do in System Preferences at a global level, but I just want to turn it off in my app.

Is there some configuration on the WKWebView, or on the app bundle itself, or somewhere, that can turn this off? Is there an HTML or CSS option? Should I be using a different type of HTML input field? Basically where in the whole stack can I intervene to turn this feature off?


回答1:


When you toggle the setting in System Preferences (Keyboard > Text > Use smart quotes and dashes), it sets a value for the key NSAutomaticQuoteSubstitutionEnabled in the user's global preferences domain. I determined this by comparing the output of defaults read -g from before to after.

You can try setting that to false for your app domain early in your app's start-up to see if that disables it for your app:

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSAutomaticQuoteSubstitutionEnabled"];

Try that in -applicationWillFinishLaunching: or even main().

Of course, if that works it will disable it throughout your app, rather than just your WKWebView.




回答2:


After some experimentation while having this issue with a <textarea> in a web view, I found out that the smart/fancy quotes replacement will be disabled by adding the spellcheck attribute.

<textarea spellcheck="false" ...>

AFAIK it is (at the writing moment) not documented anywhere and MDN currently makes no mention of spellcheck attribute "auto correcting" quotes. I'd imagine autocorrect or autocapitalize would do this, but setting these in context of a web view doesn't apparently do anything.

Since I was working on a React app at the time, the attributes are camel-cased and in that case it is <textarea spellCheck="false" ...> instead.




回答3:


If you're using Xcode's Interface Builder, open up the the utilities pain with your textfield/textbox selected. Under 'Attributes Inspector' you are able to set the Keyboard Type to ASCII.

To do so programmatically, checkout this previous question & answer.



来源:https://stackoverflow.com/questions/31455617/how-can-i-turn-off-os-xs-smart-quotes-replacement-in-my-wkwebview

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