我有一个禁用“智能引号”选项的 NSTextView:
但是,如果我要输入:
'你好世界'
进入文本视图,它立即被替换为:
'你好世界'</p>
(查看单引号是如何被替换的)。
我认为禁用智能引号会解决这个问题,但它似乎没有帮助。关于如何让这些更漂亮的报价消失的任何建议?
您可以使用以下方法禁用 NSTextView 的智能引号:
self.textView.automaticQuoteSubstitutionEnabled = NO;
自 OS X 10.9 Mavericks 起,未在界面生成器中设置复选标记似乎不起作用。
请参阅NSTextView setAutomaticQuoteSubstitutionEnabled:
您也可以使用它来更详细地控制文本替换。
self.textView.enabledTextCheckingTypes = 0;
请参阅NSTextView setEnabledTextCheckingTypes:
同样在 Mavericks 中,系统偏好设置 → 键盘 → 文本默认启用智能引号。您可以根据个人喜好禁用此功能。
从 Yosemite / Xcode 6.1 开始,此问题仍未解决
要关闭 IB 似乎忽略的所有替换选项,我必须这样做:
self.textView.automaticQuoteSubstitutionEnabled = NO;
self.textView.automaticDashSubstitutionEnabled = NO;
self.textView.automaticTextReplacementEnabled = NO;
Swift 4 扩展来制作任何 NSTextView 等宽
用法:
@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 =
//---------------------------------------------------------
}
}
斯威夫特 5 解决方案:
self.textview.smartQuotesType = .no
self.textview.smartDashesType = .no