Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView with Smart Quotes disabled, still replaces quotes

Tags:

xcode

macos

cocoa

I have a NSTextView with the 'Smart Quotes' option disabled:

Smart Quotes disabled

However, if I were to type:

'hello world'

Into the textview, it instanly gets replaced with:

‘hello world’

(see how the single quotes have been replaced).

I thought disabling smart quotes would have taken care of this, but it does not seem to of helped. Any suggestions on how I can get these prettier quotes to go away?

like image 640
Kyle Avatar asked Nov 06 '13 00:11

Kyle


4 Answers

You can disable smart quotes for your NSTextView with:

self.textView.automaticQuoteSubstitutionEnabled = NO;

Not setting the checkmark in the interface builder doesn't seem to work since OS X 10.9 Mavericks.

See NSTextView setAutomaticQuoteSubstitutionEnabled:

Also you can use this for a more detailed control over the text replacement.

self.textView.enabledTextCheckingTypes = 0;

See NSTextView setEnabledTextCheckingTypes:

Also in Mavericks there is smart quotes enabled by default in System Preferences → Keyboard → Text. You can disable this as a personal preference.

like image 102
McZonk Avatar answered Nov 01 '22 17:11

McZonk


This problem is still not fixed as of Yosemite / Xcode 6.1

To turn off all the substitution options that IB seems to ignore, I had to do this:

self.textView.automaticQuoteSubstitutionEnabled = NO;
self.textView.automaticDashSubstitutionEnabled = NO;
self.textView.automaticTextReplacementEnabled = NO;
like image 21
ElmerCat Avatar answered Nov 01 '22 16:11

ElmerCat


Xcode 7.3 still have this problem.

The way to set these properties in IB.

enter image description here

like image 6
Satachito Avatar answered Nov 01 '22 17:11

Satachito


Swift 4 extension to make any NSTextView monospace

  • good for JSON editing
  • Turns off smart quotes
  • uses monospaced font
  • can be done in IB but I had problems

Usage:

 @IBOutlet var textViewJSONPushBody: NSTextView!
 @IBOutlet var textViewConsole: NSTextView!

  ... viewDidLoad(){
         //JSON editor - larger font
         self.textViewJSONPushBody.makePlainText(withFontSize: 12.0)

         //Log view - smaller
         //defaults to sys font - small size
         self.textViewConsole.makePlainText()
         //or set fontSize
         self.textViewConsole.makePlainText(withFontSize: 10.0)
    }

NSTextView+PlainText.swift

//
//  NSTextView+PlainText.swift
//  GetStarted_NH_MacOS
//
//  Created by Brian Clear on 12/07/2018.
//  Copyright © 2018 City of London Consulting Limited. All rights reserved.
//

import Foundation
import AppKit

extension NSTextView{

    /*
     Usage:
        @IBOutlet var textViewJSONPushBody: NSTextView!
        viewDidLoad(){
            //JSON editor
            self.textViewJSONPushBody.makePlainText(withFontSize: 12.0)

            //Log view - smaller
            //defaults to sys font - small size
            self.textViewConsole.makePlainText()
            self.textViewConsole.makePlainText(withFontSize: 10.0)
        }
     */
    func makePlainText(withFontSize 
                          fontSize: CGFloat = NSFont.smallSystemFontSize)
    {

        //---------------------------------------------------------
        //FONT - monospaced
        //---------------------------------------------------------
        //small font
        //self.font = NSFont.userFixedPitchFont(ofSize: NSFont.smallSystemFontSize)

        //medium font
        self.font = NSFont.userFixedPitchFont(ofSize: NSFont.systemFontSize)

        //---------------------------------------------------------
        self.isRulerVisible = false
        self.isFieldEditor = false
        self.isRichText = false

        //---------------------------------------------------------
        //smart quotes mess up json
        //https://stackoverflow.com/questions/19801601/nstextview-with-smart-quotes-disabled-still-replaces-quotes
        self.isAutomaticQuoteSubstitutionEnabled = false

         //---------------------------------------------------------
        self.isAutomaticLinkDetectionEnabled = false
        self.isContinuousSpellCheckingEnabled = false
        self.isGrammarCheckingEnabled = false
        self.isAutomaticDashSubstitutionEnabled = false
        self.isAutomaticDataDetectionEnabled = false
        self.isAutomaticSpellingCorrectionEnabled = false
        self.isAutomaticTextReplacementEnabled = false
        self.isIncrementalSearchingEnabled = false
        self.isAutomaticTextCompletionEnabled = false
        //---------------------------------------------------------
        //see also
        //https://stackoverflow.com/questions/19801601/nstextview-with-smart-quotes-disabled-still-replaces-quotes
        //self.enabledTextCheckingTypes =
         //---------------------------------------------------------

    }
}

enter image description here

like image 4
brian.clear Avatar answered Nov 01 '22 16:11

brian.clear