开发者

Text formatting in NSTextField

I have a graphics app that uses a NSTextField instance for in-place text editing, this feature was added long time ago and I never had a reason to check it, however, I have recently received a report: text filed doesn't allow text formatting. Format -> Text menu subitems are all d开发者_开发知识库isabled, so there is no way to set the paragraph of a text item.

The question: how should I set up the NSTextField to support paragraph editing? I'm sure it did work before since I have some projects with formatted text and NSTextField was there since the app was born. Did I miss something with system/XCode updates?

My NSTextField is multiline, editable, allowed to edit text attributes.


If someone will face this in the future, I can describe the problem in details:

  1. NSTextView refuses to apply formatting if there is no ruler used (with recent OS update, I suppose)
  2. NSTextField itself does no text editing, it uses shared NSTextView instance driven by the owning NSWindow
  3. Default text editor from the NSWindow doesn't use rulers.

This results disabled text formatting when using NSTextField.

Solution is to subclass the NSWindow:

@implementation MyWindow

- (NSText *)fieldEditor:(BOOL)createWhenNeeded forObject:(id)anObject
{
    NSText* text = [super fieldEditor:createWhenNeeded forObject:anObject];
    if ([text isKindOfClass:[NSTextView class]])
        [(NSTextView *)text setUsesRuler:YES];
    return text;
}

@end

And voila, formatting is back.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜