gpt4 book ai didi

objective-c - NSCell 上的 NSAttributedString

转载 作者:行者123 更新时间:2023-12-03 16:27:38 24 4
gpt4 key购买 nike

我正在使用 NSAttributedString 格式化的数据填充 NSOutlineView。到目前为止,我已经设置了文本字体、大小和颜色的格式。我的问题是,选择行时前景色不会改变。如果您创建一个 NSTextFieldCell 并将颜色设置为 Interface Builder 上的 disabledControlTextColor,则它可以正常工作:未选择时,它显示为灰色,选择白色时,当我以编程方式将此颜色设置为属性字符串定义时,它始终显示为灰色。

NSMutableAttributedString *result = [[[NSMutableAttributedString alloc] initWithString:value] autorelease];
NSDictionary *attributes = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont systemFontSize] -1], NSFontAttributeName,
[NSColor disabledControlTextColor], NSForegroundColorAttributeName, nil] retain];

[result addAttributes:attributes range:[value rangeOfString:value]];

提前致谢。

最佳答案

当子类化 NSCell 时,在设置文本字段值时,我们应该询问单元格是否突出显示,然后设置文本的前景色。

NSString *titleValue = @"TEST";
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:titleValue];
NSColor *color = [self isHighlighted] ? [NSColor whiteColor] : [NSColor blackColor];
NSDictionary *attributes = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont boldSystemFontOfSize:[NSFont systemFontSize] + 1], NSFontAttributeName,
color, NSForegroundColorAttributeName, nil] autorelease];
[titleString addAttributes:attributes range:[titleValue rangeOfString:titleValue]];
[self setAttributedStringValue:value];

关于objective-c - NSCell 上的 NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6576031/

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