1

我有一个表格视图,在该表格视图上选择一行的一列将调用重写方法

- (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;
}

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

4

1 回答 1

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])];
}
于 2012-07-13T11:57:06.217 回答