gpt4 book ai didi

objective-c - 如何实现事件时带有下划线文本的标签按钮

转载 作者:行者123 更新时间:2023-12-03 17:40:04 25 4
gpt4 key购买 nike

我已在 View 中插入了两个标签。当我单击其中一个标签时,我想运行一些隐藏代码(更改 subview ),并且标签中的文本应带有下划线。

这应该如何在 Cocoa 中实现?

最佳答案

子类化 NSTextField 并实现其 mouseDown: 事件

@interface ClickableLabel : NSTextField

@end


@implementation ClickableLabel

- (void)mouseDown:(NSEvent *)theEvent
{
[super mouseDown:theEvent];

NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: [self stringValue]];
NSRange range = NSMakeRange(0, [attrString length]);

[attrString beginEditing];

// make the text appear with an underline
[attrString addAttribute: NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];

[attrString endEditing];

[self setAttributedStringValue:attrString];

[attrString release];
}

@end

将此ClickableLabel设置为标签的类

关于objective-c - 如何实现事件时带有下划线文本的标签按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19692728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com