Mac OS X: Strike Out Shortcut Text (NSTextField)

Is it possible to cross out text in a label ( NSTextField )?

I tried using the font panel, but apparently they are ignored when I try to install them:

enter image description here

enter image description here

+4
source share
2 answers

You can do this like this, assuming _textField set as the output in your xib:

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

Edit:

If you want to write your own NSTextFieldCell subheading instead, the only way you will need to override is 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]]; } 
+5
source

For me, this goes well with sbooth's approach to creating a custom NSTextFieldCell and overriding drawInteriorWithFrame:inView: as shown below:

 - (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]; } 
+1
source

All Articles