1

したがって、この質問には実際には 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
4

1 に答える 1

6

必要なものを実装するには、おそらく NSTextField をサブクラス化する必要があります。やった。

1. 下線

NSAttributedString に拡張関数を作った (NSAttributedString+Hyperlink.m)

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

そして、次のようにタイトルを NSTextField (ラベル) に割り当てることができます。

[myTextField setAttributedStringValue:[NSAttributedString fakeHyperlinkFromString:@"Hello world!" withColor:[NSColor blueColor]]];



2. NSTextField -> アクションの送信をクリックします。

ここでは、デリゲートを使用して performSelector を実行できます。NSTextField サブクラスの h ファイルでデリゲートを宣言します。

@property (assign) IBOutlet id delegate;

@synthesizemファイルに対応しています。これで、InterfaceBuilder (xib-file) でデリゲートを接続 (割り当て) できるようになりました。

その後、NSTextField サブクラスの mouseUp (または mouseDown) メソッドを実装できます。

- (void)mouseUp:(NSEvent *)theEvent {

    [super mouseUp:theEvent];

    // call delegate
    if (delegate != nil && [delegate respondsToSelector:@selector(textFieldWasClicked)] {

        [delegate performSelector:@selector(textFieldWasClicked) withObject:nil];
    }
}

それは私のために働いた。次はあなたが試して。


アップデート

fakeHyperlinkFromStringNSAttributedStringへのカテゴリとして NSAttributedString+Hyperlink.mに配置する必要があります。

Hファイル:

#import <Foundation/Foundation.h>

@interface NSAttributedString (Hyperlink)

+(id)fakeHyperlinkFromString:(NSString*)inString withColor:(NSColor*)color;

@end



M-ファイル:

#import "NSAttributedString+Hyperlink.h"

@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

そして、使用する場所にこの NSAttributedString+Hyperlink.h を含めるだけですfakeHyperlinkFromString。通常はコントローラー(ウィンドウコントローラー)です。

このコントローラーには、textField オブジェクト (作成した場合はサブクラス) へのポインターが必要です。これは、 and で宣言し、それを合成して InterfaceBuilder で接続することで実行でき@propertyます。(assign)IBOutlet

于 2012-07-14T21:36:39.553 に答える