gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:14:33 26 4
gpt4 key购买 nike

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

- (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:...]方法后,文本属性的更改将得到反射(reflect)。解决办法如下:

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

关于cocoa - 选择时 NSTextFieldCell 的文本属性已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10978822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com