选择时,NSTextFieldCell的文本属性已更改

时间:2012-06-11 10:59:50

标签: cocoa nstextfieldcell fieldeditor

我有一个tableview,选择一行的列将调用重写方法

- (void)selectWithFrame:(NSRect)inCellFrame inView:(NSView *)inControlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
  // here do some text positioning
         [super selectWithFrame:inCellFrame inView:inControlView editor:textObj delegate:anObject start:selStart length:selLength];
}

我还返回单元格的字段编辑器,如下所示:

- (NSTextView *)fieldEditorForView:(NSView *)inControlView {
   MYTextView* fieldEditor = [[[MYTextView alloc] initWithFrame:NSZeroRect] autorelease];
   return fieldEditor;
}

选择单元格后,单元格中的文本会更改其属性。比如,字体大小,字体,字体样式等相应地改变,似乎当文本处于选择模式时我无法控制它。即使在选择后我也不想更改其字体属性如何避免文本属性中的这种更改?

1 个答案:

答案 0 :(得分:1)

调用-[super selectWithFrame:...]方法后,将反映对文本属性的更改。解决方案如下:

- (void)selectWithFrame:(NSRect)inCellFrame
                 inView:(NSView *)inControlView
                 editor:(NSText *)textObj
               delegate:(id)anObject
                  start:(NSInteger)selStart
                 length:(NSInteger)selLength {
    // here do some text positioning

    [super selectWithFrame:inCellFrame
                    inView:inControlView
                    editor:textObj
                  delegate:anObject
                     start:selStart
                    length:selLength];

    NSMutableAttributedString* text = [textObj textStorage];

    NSMutableParagraphStyle *alignmentStyle = [[NSParagraphStyle defaultParagraphStyle] autorelease];
    [alignmentStyle setAlignment:NSLeftTextAlignment];

    NSDictionary* attributes = [NSMutableDictionary dictionary];
    [attributes setValue:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]] forKey:NSFontAttributeName];
    [attributes setValue:leftAlignmentStyle forKey:NSParagraphStyleAttributeName];

    [text setAttributes:attributes range:NSMakeRange(0, [text length])];
}