如何处理NSTextView首选项(拼写和语法,替换等) [英] How to handle NSTextView preferences (spelling and grammar, substitutions,...)
问题描述
NSTextView
类允许用户使用上下文菜单(右键单击)禁用/启用键入时进行拼写等功能。但是,当我在自己的应用程序中使用 NSTextView
时,文本视图本身不会自动保存这些首选项,这意味着我必须分别保存它们-对吗?现在,我还想允许用户在我的应用偏好设置中更改这些设置(例如在TextEdit中)。我要做的是将文本视图首选项保存为用户默认设置,这意味着每次用户在应用程序首选项中更改设置时,我都会应用这些设置并将其保存。除了用户通过上下文菜单而不是通过应用程序首选项更改文本视图设置的一种情况外,很容易做到。
我现在的问题是:更改 NSTextView
的设置时会收到通知,因此我可以保存它们吗?
我已经完成了一个项目,在该项目中,子类化了NSTextView,并且我可以轻松地捕获由
因此,您只需创建一个新的 .h
& .m
文件并这样声明:
(在.h文件中)
@interface BrutellaTextView:NSTextView
@end
(在.m文件中)
@interface BrutellaTextView
-(void)setContinuousSpellCheckingEnabled:(BOOL)flag
{
//这里我只是设置用户默认值...您可以选择
// //保存其他位置
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
if(userDefaults)
{
[userDefaults setBool:flag forKey:@ continuousSpellCheckingEnabled];
}
//并使其实际发生,
//调用超类
[super setContinuousSpellCheckingEnabled:flag];
}
@end
(您可以覆盖其他 NSTextView
方法来捕获其他设置的更改,例如 setRulerVisible:
)。
现在,当您处于XIB文件中时,请确保将文本视图的CustomClass设置为 BrutellaTextView
,您将被设置好!
没有通知您可以注册以获取NSTextView设置更改,所以就我而言,这是最好的方式去做你想做的事。
我希望这个答案可以帮助您!
The NSTextView
class allows the user to dis-/enable features like "spelling while typing" with the context menu (right click). But when I use a NSTextView
in my own app, those preferences are not saved automatically by the text view itself, which means that I have to save them separately - right?
Now I also want to allow the user to change those settings in my app preferences (like in TextEdit). What I do is to save the text view preferences in the user defaults, which means that every time the user changes the setting in the app preferences, I apply those settings and save them. It's pretty easy to accomplish that except the one case where the user changes the text view setting with the context menu and not through the app preferences.
My question now: How can I get notified when the settings of a NSTextView
is changed, so I can save them?
I've done a project where I have subclassed NSTextView and I can easily catch any setting changes that have been made by the user.
So, for you to do this, simply create a new .h
& .m
file and declare it like this:
(in the .h file)
@interface BrutellaTextView : NSTextView
@end
(in the .m file)
@interface BrutellaTextView
- (void)setContinuousSpellCheckingEnabled:(BOOL)flag
{
// here I am just setting user defaults... you may choose
// to have some other place to save the settings
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
if(userDefaults)
{
[userDefaults setBool: flag forKey: @"continuousSpellCheckingEnabled"];
}
// and to get the functionality to actually happen,
// call the superclass
[super setContinuousSpellCheckingEnabled: flag];
}
@end
(and you can override other NSTextView
methods to capture when other settings change, such as setRulerVisible:
).
Now, when you are in your XIB file, make sure to set the CustomClass of your text view to be BrutellaTextView
and you'll be all set!
There are no notifications that you can register to get NSTextView settings changes, so as far as I'm concerned, this is the best way to do what you're trying to do.
I hope this answer helps you out!
这篇关于如何处理NSTextView首选项(拼写和语法,替换等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!