NSCell 上的 NSAttributedString

发布于 2024-11-18 08:07:23 字数 742 浏览 4 评论 0原文

我正在使用 NSAttributedString 格式化的数据填充 NSOutlineView。到目前为止,我已经设置了文本字体、大小和颜色的格式。我的问题是,选择行时前景色不会改变。如果您创建 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]];

提前致谢。

I'm filling an NSOutlineView with data that I format using NSAttributedString. So far I have formatted the text font, size and color. My problem is that the foreground color doesn't change when the row is selected. If you create an NSTextFieldCell and set the color to disabledControlTextColor on the Interface Builder, it works fine: When not selected it is show as gray, and when selected white, when I programmatically set this color to the attributed string definition it is always show as gray.

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]];

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴随着你 2024-11-25 08:07:23

当子类化 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];

When subclassing NSCell, when setting the textfield value, we should ask if the cell isHighlighted and then set the foreground color of the text.

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];
打小就很酷 2024-11-25 08:07:23

在自定义单元格中使用它,我尝试了互联网上的所有内容,最后下面的东西起作用了

- (void)updateCellDisplay {
  if (self.selected || self.highlighted) {
  self.nameLabel.textColor = [UIColor lightGrayColor];
  self.colorLabel.textColor = [UIColor lightGrayColor];
  }
  else {
   self.nameLabel.textColor = [UIColor blackColor];
   self.colorLabel.textColor = [UIColor blackColor];
  }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  [super setHighlighted:highlighted animated:animated];
  [self updateCellDisplay];
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];
  [self updateCellDisplay];
}

use this in the custom cell, i tried everything on the internet and finally below thing worked

- (void)updateCellDisplay {
  if (self.selected || self.highlighted) {
  self.nameLabel.textColor = [UIColor lightGrayColor];
  self.colorLabel.textColor = [UIColor lightGrayColor];
  }
  else {
   self.nameLabel.textColor = [UIColor blackColor];
   self.colorLabel.textColor = [UIColor blackColor];
  }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  [super setHighlighted:highlighted animated:animated];
  [self updateCellDisplay];
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];
  [self updateCellDisplay];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文