Mac OS X:删除标签中的文本(NSTextField)(Mac OS X: strikethrough the text in a label (NSTextField))

编程入门 行业动态 更新时间:2024-10-25 06:21:25
Mac OS X:删除标签中的文本(NSTextField)(Mac OS X: strikethrough the text in a label (NSTextField))

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

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

Is it possible to strikethrough the text in a label (NSTextField)?

I have tried to use the Font Panel, but apparently these are ignored when I try to set them:

最满意答案

你可以这样做,假设_textField被设置为_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:

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

You can do it like this, assuming _textField is set as an outlet 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 a custom strikethrough NSTextFieldCell subclass instead, the only method that should be necessary 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]]; }

更多推荐

本文发布于:2023-08-08 01:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   标签   OS   Mac   label

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!