是否可以删除标签 ( NSTextField
) 中的文本?
我曾尝试使用字体面板,但显然当我尝试设置它们时会忽略它们:
是否可以删除标签 ( NSTextField
) 中的文本?
我曾尝试使用字体面板,但显然当我尝试设置它们时会忽略它们:
您可以这样做,假设_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]];
}
对我来说,通过 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];
}