Objective-c鼠标悬停时的字段标签
所以这个问题实际上包括两个问题Objective-c鼠标悬停时的字段标签,objective-c,xcode,macos,cocoa,Objective C,Xcode,Macos,Cocoa,所以这个问题实际上包括两个问题 首先,我以编程方式创建了大约5个NSTextField标签,我想知道当我悬停标签时,如何给它们加下划线,相应的标签将加下划线?我在这件事上被难住了,所以我甚至不知道该怎么做。我知道大多数人都会认为我发疯了,甚至会问下一个问题,但我确实有我这样做的理由 第二,如何将单击附加到NSTextField标签?我不想使用按钮,因为我不想在单击按钮时背景色可见,即使我隐藏了边框,单击按钮时仍然可以看到背景色。我环顾四周(stackoverflow.com和谷歌),似乎没有人
- 首先,我以编程方式创建了大约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)];
您可能应该对NSTextField进行子类化,以实现所需的功能。是的 1.强调 我对NSAttributedString做了一个扩展函数(NSAttributedString+Hyperlink.m) 然后可以将标题指定给NSTextField(标签),如下所示:
[myTextField setAttributedStringValue:[NSAttributedString fakeHyperlinkFromString:@"Hello world!" withColor:[NSColor blueColor]]];
2.单击NSTextField->send action 在这里,您可以使用委托对其执行选择器。 在NSTextField子类的h文件中声明委托:
@property (assign) IBOutlet id delegate;
- (void)mouseUp:(NSEvent *)theEvent {
[super mouseUp:theEvent];
// call delegate
if (delegate != nil && [delegate respondsToSelector:@selector(textFieldWasClicked)] {
[delegate performSelector:@selector(textFieldWasClicked) withObject:nil];
}
}
与m文件中相应的@synthesis
。
现在,您可以在IntefaceBuilder(xib文件)中连接(分配)委托
之后,可以实现NSTextField子类的mouseUp(或mouseDown)方法:
@property (assign) IBOutlet id delegate;
- (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-File:
#import <Foundation/Foundation.h>
@interface NSAttributedString (Hyperlink)
+(id)fakeHyperlinkFromString:(NSString*)inString withColor:(NSColor*)color;
@end
#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对象的指针(如果您创建了一个子类)。这可以通过使用
(assign)
和IBOutlet
声明@property
,合成它并在InterfaceBuilder中连接来实现。您可能应该对NSTextField进行子类化,以实现所需的功能。是的
1.强调
我对NSAttributedString做了一个扩展函数(NSAttributedString+Hyperlink.m)
然后可以将标题指定给NSTextField(标签),如下所示:
[myTextField setAttributedStringValue:[NSAttributedString fakeHyperlinkFromString:@"Hello world!" withColor:[NSColor blueColor]]];
2.单击NSTextField->send action 在这里,您可以使用委托对其执行选择器。 在NSTextField子类的h文件中声明委托:
@property (assign) IBOutlet id delegate;
- (void)mouseUp:(NSEvent *)theEvent {
[super mouseUp:theEvent];
// call delegate
if (delegate != nil && [delegate respondsToSelector:@selector(textFieldWasClicked)] {
[delegate performSelector:@selector(textFieldWasClicked) withObject:nil];
}
}
与m文件中相应的@synthesis
。
现在,您可以在IntefaceBuilder(xib文件)中连接(分配)委托
之后,可以实现NSTextField子类的mouseUp(或mouseDown)方法:
@property (assign) IBOutlet id delegate;
- (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-File:
#import <Foundation/Foundation.h>
@interface NSAttributedString (Hyperlink)
+(id)fakeHyperlinkFromString:(NSString*)inString withColor:(NSColor*)color;
@end
#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对象的指针(如果您创建了一个子类)。这可以通过使用
(assign)
和IBOutlet
声明@property
,合成它并在InterfaceBuilder中连接来实现。感谢您给出了非常完整的答案。我问了一些问题,我把+(id)fakeHyperlinkFromString:(NSString*)inString with color:(NSColor*)color{}
放在哪里,比如我把它放在哪个文件中,MainWindowController.h或MainWindowController.m?另外,我应该在写有myTextField
的地方放什么,文本字段的名称?正如你们所知,我对Objective-C有点陌生,而且随着我的学习,你们能解释一下子视图吗?谢谢我已经更新了答案。阅读有关类别的更多信息:首先-您最好编写@nsattributestring(Hyperlink)
是一个名为nsattributestring+Hyperlink.m的单独文件。您确实包含了头文件,所以请利用这些优点;)第二,当执行alloc/init时出现内存泄漏,然后将变量保留在自己的位置,下次再次分配内存。您应该将其分配给一个变量,并在不需要时释放它,或者使用autorelease方法告诉autorelease池稍后释放内存。第三,在drawRect方法中创建字符串是一个糟糕的设计。创建两个名为text_one和text_two的属性(或私有类变量),并将字符串存储在其中。使用操作或委托调用更新字符串值/选项(您可以将attributedString指定给文本字段的同一点)。然后让drawRect绘制当前存储的内容。感谢您给出了非常完整的答案。我问了一些问题,我把+(id)fakeHyperlinkFromString:(NSString*)inString with color:(NSColor*)color{}
放在哪里,比如我把它放在哪个文件中,MainWindowController.h或MainWindowController.m?另外,我应该在写有myTextField
的地方放什么,文本字段的名称?正如你们所知,我对Objective-C有点陌生,而且随着我的学习,你们能解释一下子视图吗?谢谢我已经更新了答案。阅读有关类别的更多信息:首先-您最好编写@nsattributestring(Hyperlink)
是一个名为nsattributestring+Hyperlink.m的单独文件。您确实包含了头文件,所以请利用这些优点;)第二,当执行alloc/init时出现内存泄漏,然后将变量保留在自己的位置,下次再次分配内存