所以这个问题实际上涉及2个问题。
- 首先,我以编程方式创建了大约 5 个 NSTextField 标签,我想知道当我悬停标签时如何给它们加下划线,相应的标签会加下划线吗?我对此感到困惑,所以我什至不知道如何去做。我知道大多数人会认为我连问下一个问题都疯了,但我确实有这样做的理由。
其次,如何将点击附加到 NSTextField 标签?我不想使用按钮,因为我不希望单击它时背景颜色可见,即使我隐藏了边框,单击时仍然可以看到背景颜色。我环顾四周(stackoverflow.com 和谷歌),似乎没有人回答这些问题。你可能需要我如何绘制 NSTextField 标签的代码,所以你可以开始了。
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica" size:14], NSFontAttributeName,[NSColor lightGrayColor], NSForegroundColorAttributeName, nil]; NSAttributedString * trashText=[[NSAttributedString alloc] initWithString: @"Trash" attributes: attributes]; [trashText drawAtPoint:NSMakePoint(20, 500)];
更新
添加了更多的objective-c。
主窗口控制器.h
#import <Cocoa/Cocoa.h>
@interface MainWindowController : NSWindowController {
@private
}
@end
@interface EmailContents : NSView {
@private
}
@end
@interface SideBar : NSView {
@private
}
@end
@interface ClickableTextField : NSTextField
@end
主窗口控制器.m
#import "MainWindowController.h"
#import "NSAttributedString+Hyperlink.h"
int currentView = 1;
@implementation NSAttributedString (Hyperlink)
+(id)fakeHyperlinkFromString:(NSString*)inString withColor:(NSColor*)color {
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: inString];
NSRange range = NSMakeRange(0, [attrString length]);
[attrString beginEditing];
// make the text appear in color
[attrString addAttribute:NSForegroundColorAttributeName value:color range:range];
// make the text appear with an underline
[attrString addAttribute: NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];
[attrString endEditing];
return [attrString autorelease];
}
@end
@implementation SideBar
- (void)drawRect:(NSRect)HTMLContent {
int height = [[NSScreen mainScreen] frame].size.height;
if (currentView == 1) {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AvenirNext-Regular" size:16], NSFontAttributeName,[NSColor whiteColor], NSForegroundColorAttributeName, nil];
NSAttributedString * text_one=[[NSAttributedString alloc] initWithString: @"text_one" attributes: attributes];
//[text_one setAttributedStringValue:[NSAttributedString fakeHyperlinkFromString:@"text_one" withColor:[NSColor whiteColor]]];
[text_one drawAtPoint:NSMakePoint(20, 600)];
} else {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AvenirNext-UltraLight" size:14], NSFontAttributeName,[NSColor darkGrayColor], NSForegroundColorAttributeName, nil];
NSAttributedString * text_one=[[NSAttributedString alloc] initWithString: @"text_one" attributes: attributes];
[text_one drawAtPoint:NSMakePoint(20, 600)];
}
if (currentView == 2) {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AvenirNext-Regular" size:16], NSFontAttributeName,[NSColor whiteColor], NSForegroundColorAttributeName, nil];
NSAttributedString * text_two=[[NSAttributedString alloc] initWithString: @"text_two" attributes: attributes];
[text_two drawAtPoint:NSMakePoint(20, 575)];
} else {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AvenirNext-UltraLight" size:14], NSFontAttributeName,[NSColor darkGrayColor], NSForegroundColorAttributeName, nil];
NSAttributedString * text_two=[[NSAttributedString alloc] initWithString: @"text_two" attributes: attributes];
[text_two drawAtPoint:NSMakePoint(20, 575)];
}
...
}
@end