【问题标题】:Text attribute changed for NSTextFieldCell on selection选择时更改了 NSTextFieldCell 的文本属性
【发布时间】:2012-06-11 10:59:50
【问题描述】:

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

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

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

【问题讨论】:

    标签: cocoa nstextfieldcell fieldeditor


    【解决方案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])];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      相关资源
      最近更新 更多