mik*_*wan 2 macos cocoa objective-c nsattributedstring nscell
我正在使用我使用NSAttributedString格式化的数据填充NSOutlineView.到目前为止,我已经格式化了文本字体,大小和颜色.我的问题是,选择行时前景色不会改变.如果您在Interface Builder上创建NSTextFieldCell并将颜色设置为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]];
Run Code Online (Sandbox Code Playgroud)
提前致谢.
当子类化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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4094 次 |
| 最近记录: |