在NSTextView中设置默认字体

Jos*_*ler 15 cocoa

如何设置NSTextView组件的默认字体?我需要在我的应用程序中有一个区域,用户可以在其中更改纯文本文件,我希望该字体是等宽字体.

Jos*_*ler 25

找到了.这个答案很接近,但不会这样做 - 必须首先访问NSTextView的textStorage对象:

[[myNSTextView textStorage] setFont:[NSFont fontWithName:@"Menlo" size:11]];
Run Code Online (Sandbox Code Playgroud)

  • 从NSText(NSTextView的超类)文档:"将所有接收者文本的字体设置为<var> aFont </ var>."如果这对您不起作用,您应该查看原因. (5认同)
  • 此外,您应该使用用户首选的固定间距字体和大小,而不是硬编码的字体和大小.http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSFont_Class/Reference/Reference.html#//apple_ref/occ/clm/NSFont/userFixedPitchFontOfSize: (3认同)
  • 不知道为什么,但这对我不起作用,即使`[myNSTextView setFont:[NSFont fontWithName:@"Menlo"size:11]]`也行. (3认同)
  • 我还必须删除'textStorage'才能让它工作 (2认同)

Dew*_*sen 16

NSFont* font = <whatever>;
[textView setFont: font];