したがって、この質問には実際には 2 つの質問が含まれます。
- まず、プログラムで約 5 つの NSTextField ラベルを作成しました。ラベルにカーソルを合わせると、対応するラベルに下線が引かれます。私はこれに困惑しているので、どうやってそれを行うのかさえわかりません。ほとんどの人が、次の質問をすることさえ私がクレイジーだと思うだろうと知っていますが、私にはそれをする理由があります.
次に、クリックを NSTextField ラベルに添付するにはどうすればよいですか? ボタンをクリックしたときに背景色が表示されないようにするため、ボタンを使用したくありません。境界線を非表示にしても、クリックすると背景色が表示されます。私は(stackoverflow.comとgoogle)見回しましたが、これらの質問のいずれにも答えがないようです。おそらく、私が 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 を追加しました。
MainWindowController.h
#import <Cocoa/Cocoa.h>
@interface MainWindowController : NSWindowController {
@private
}
@end
@interface EmailContents : NSView {
@private
}
@end
@interface SideBar : NSView {
@private
}
@end
@interface ClickableTextField : NSTextField
@end
MainWindowController.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