Objective-c在悬停时强调NSTextField标签

时间:2012-07-14 20:37:09

标签: objective-c xcode macos cocoa

所以这个问题实际上涉及两个问题。

  • 首先,我已经以编程方式创建了大约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。

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

1 个答案:

答案 0 :(得分: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 - &gt;发送行动

在这里你可以使用delegate来执行performSelector。 在NSTextField子类的h文件中声明委托:

@property (assign) IBOutlet id delegate;

在m文件中包含相应的@synthesize。 现在,您可以在IntefaceBuilder(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];
    }
}

这对我有用。现在你试试。


<强>更新

您应该将fakeHyperlinkFromString作为NSAttributedString的类别添加到 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

然后只使用fakeHyperlinkFromString包含此NSAttributedString + Hyperlink.h。 它通常是一个控制器(窗口控制器)。

在这个控制器中,你应该有一个指向textField对象的指针(如果你创建了一个子类,则为子类)。这可以通过使用@property(assign)声明IBOutlet,合成它并在InterfaceBuilder中连接来完成。