首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSCell上的NSAttributedString

NSCell上的NSAttributedString
EN

Stack Overflow用户
提问于 2011-07-05 05:02:03
回答 2查看 4.2K关注 0票数 2

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

代码语言:javascript
复制
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]];

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-07 21:05:52

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

代码语言:javascript
复制
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];
票数 5
EN

Stack Overflow用户

发布于 2013-01-03 04:55:03

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

代码语言:javascript
复制
- (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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6576031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档