简体   繁体   中英

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

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?

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM