2012-06-11 38 views
0

我有一個的tableview上選擇行的一列將調用重寫的方法文本屬性改變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; 
} 

當選中單元格時,單元格內的文本將更改其屬性。就像字體大小,字體大小,字體樣式等相應地發生變化,當文本處於選擇模式時,似乎我無法控制它。我不想在選擇後更改其字體屬性如何避免文本屬性中的這種更改?

回答

0

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