设置 NSTextView 的字体
我正在子类化 NSTextView 以设置它的默认字体:
PTNfoEditField.h
@interface PTNfoEditField : NSTextView {
}
@end
PTNfoEditField.m
@implementation PTNfoEditField
-(id)init {
if(self = [super init]) {
[[self textStorage] setFont:[NSFont fontWithName:@"Courier" size:10]];
}
return self;
}
@end
但是,这不起作用,我不知道如何以不同的方式做到这一点。谁能帮助我吗?谢谢。
I am subclassing NSTextView to set it's default font:
PTNfoEditField.h
@interface PTNfoEditField : NSTextView {
}
@end
PTNfoEditField.m
@implementation PTNfoEditField
-(id)init {
if(self = [super init]) {
[[self textStorage] setFont:[NSFont fontWithName:@"Courier" size:10]];
}
return self;
}
@end
However, this does not work and I don't know how to do it in a different way. Can anyone help me? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视图的初始值设定项不是
init
,而是initWithFrame:
。此外,如果您的视图位于笔尖中,您还必须重写initWithCoder:
。不要忘记确保您的视图实际上是 PTNfoEditField,而不是常规的 NSTextView。另外,您应该使用
[NSFont userFixedPitchFontOfSize:0.0]
来设置字体。您不应对字体名称(用户可能更喜欢更好的等宽字体)和字体大小(用户可能会发现 10 磅难以阅读或将抗锯齿阈值设置在其上方)进行硬编码。The initializer for views is not
init
, it'sinitWithFrame:
. Furthermore, if your view is in a nib, you must also overrideinitWithCoder:
. Don't forget to make sure your view is actually a PTNfoEditField, not a regular NSTextView.Also, you should use
[NSFont userFixedPitchFontOfSize:0.0]
to set the font. You should hard-code neither the font name (the user may prefer a better monospaced font) nor the font size (the user may find 10-pt hard to read or have their anti-aliasing threshold set above it).也许也可以尝试
setTypingAttributes:
。Maybe try
setTypingAttributes:
too.