我在视图中插入了两个标签。当我单击其中一个标签时,我想在代码后面运行一些(更改子视图),并且标签中的文本应加下划线。
如何在可可中实施?
最佳答案
子类化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/