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



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

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

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

调用-[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])];
}

最新更新