5

是否可以删除标签 ( NSTextField) 中的文本?

我曾尝试使用字体面板,但显然当我尝试设置它们时会忽略它们:

在此处输入图像描述

在此处输入图像描述

4

2 回答 2

6

您可以这样做,假设_textField在您的 xib 中设置为插座:

- (void) awakeFromNib
{
  NSMutableAttributedString *as = [[_textField attributedStringValue] mutableCopy];
  [as addAttribute:NSStrikethroughStyleAttributeName value:(NSNumber *)kCFBooleanTrue range:NSMakeRange(0, [as length])];
  [_textField setAttributedStringValue:[as autorelease]];
}

编辑:

如果您想编写自定义删除线NSTextFieldCell子类,则唯一需要覆盖的方法是setStringValue:

- (void) setStringValue:(NSString *)aString
{
  NSMutableAttributedString *as = [[NSMutableAttributedString alloc] initWithString:aString];
  [as addAttribute:NSStrikethroughStyleAttributeName value:(NSNumber *)kCFBooleanTrue range:NSMakeRange(0, [as length])];
  [self setAttributedStringValue:[as autorelease]];
}
于 2011-09-03T15:59:46.743 回答
1

对我来说,通过 sbooth 创建自定义NSTextFieldCell和覆盖的方法结合起来效果很好drawInteriorWithFrame:inView:,如下所示:

- (void) drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [self setAttributedStringFromStringValue];
    [super drawInteriorWithFrame:cellFrame inView:controlView];
}


- (void) setAttributedStringFromStringValue {  // add strikethrough
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.stringValue];
    [attributedString addAttribute:NSStrikethroughStyleAttributeName value:(NSNumber *)kCFBooleanTrue range:NSMakeRange(0, attributedString.length)];
    [self setAttributedStringValue:attributedString];
}
于 2013-06-10T22:53:27.723 回答