Mac OS X:删除标签中的文本(NSTextField)

时间:2011-09-03 14:44:42

标签: cocoa macos interface-builder nstextfield strikethrough

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

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

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:6)

你可以这样做,假设{x}被设置为xib中的插座:

_textField

编辑:

如果您想要编写自定义删除线- (void) awakeFromNib { NSMutableAttributedString *as = [[_textField attributedStringValue] mutableCopy]; [as addAttribute:NSStrikethroughStyleAttributeName value:(NSNumber *)kCFBooleanTrue range:NSMakeRange(0, [as length])]; [_textField setAttributedStringValue:[as autorelease]]; } 子类,则唯一需要覆盖的方法是NSTextFieldCell

setStringValue:

答案 1 :(得分:1)

对我而言,通过创建自定义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];
}
相关问题