xcode 悬停时,Objective-c 在 NSTextField 标签下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11487214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:54:05  来源:igfitidea点击:

Objective-c underline NSTextField Label on hover

objective-cxcodemacoscocoa

提问by jtorraca

So this question actually involves 2 questions.

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

  • First, I have programmatically created about 5 NSTextField Labels and I was wondering how I could underline them when I hover a label, the corresponding label will underline? I have been stumped on this so I have no clue how to even go about doing it. I know most people are going to think I am crazy for even asking the next question, but I do have my reasons for doing it.
  • Second, How can I attach a click to an NSTextField Label? I don't want to use a button because I don't want the background color to be visible when I click it, even if I hide the border, there is still a background color visible on click. I have looked around(stackoverflow.com and google) and nobody seems to have an answer to either of these questions. You will probably need the code of how I drew the NSTextField Labels so here you go.

    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)];
    
  • 首先,我以编程方式创建了大约 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)];
    

UPDATE

更新

Added more objective-c.

添加了更多的目标-c。

MainWindowController.h

主窗口控制器.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

主窗口控制器.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

回答by UJey

You should probably subclass the NSTextField to implement what you want. I did.

您可能应该继承 NSTextField 以实现您想要的。我做到了。

1. Underline

1. 下划线

I made an extention function to the NSAttributedString (NSAttributedString+Hyperlink.m)

我对 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];
}

And then you can assign the title to the NSTextField (label) like this:

然后你可以像这样将标题分配给 NSTextField(标签):

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





2. Click on NSTextField -> send action

2. 点击 NSTextField -> 发送动作

Here you can use delegate to performSelector on it. Declare delegate in h-file of NSTextField subclass:

在这里,您可以使用委托对其执行选择器。在 NSTextField 子类的 h 文件中声明委托:

@property (assign) IBOutlet id delegate;

with corresponding @synthesizein m-file. Now you can connect (assign) delegate in IntefaceBuilder (xib-file).

@synthesizem 文件中的对应。现在您可以在 IntefaceBuilder(xib 文件)中连接(分配)委托。

After that you can implement mouseUp (or mouseDown) method of NSTextField subclass:

之后,您可以实现 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];
    }
}

That worked for me. Now you try.

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



UPDATE

更新

You should put fakeHyperlinkFromStringto the NSAttributedString+Hyperlink.mas a category to the NSAttributedString.

H-File:

你应该把fakeHyperlinkFromStringNSAttributedString + Hyperlink.m作为一个类别的NSAttributedString。

H文件:

#import <Foundation/Foundation.h>

@interface NSAttributedString (Hyperlink)

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

@end



M-File:



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

And then just include this NSAttributedString+Hyperlink.h where you use fakeHyperlinkFromString. It is usualy a controller (window controller).

In this controller you should have a pointer to your textField object (subclass if you created one). This can be done by declaring @propertywith (assign)and IBOutlet, synthesizing it and connecting in InterfaceBuilder.

然后在你使用的地方包含这个 NSAttributedString+Hyperlink.h fakeHyperlinkFromString。它通常是一个控制器(窗口控制器)。

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