gpt4 book ai didi

Objective-c 悬停时为 NSTextField 标签下划线

转载 作者:行者123 更新时间:2023-12-03 16:51:22 24 4
gpt4 key购买 nike

所以这个问题实际上涉及到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

最佳答案

您可能应该子类化 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 -> 发送操作

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

@property (assign) IBOutlet id delegate;

与m文件中相应的@synthesize。现在您可以在 IntefaceBuilder(xib 文件)中连接(分配)委托(delegate)。

之后您可以实现 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+Hyperlink.m 作为 NSAttributedString 的类别。

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 即可。它通常是一个 Controller (窗口 Controller )。

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

关于Objective-c 悬停时为 NSTextField 标签下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11487214/

24 4 0