【问题标题】:Objective-c underline NSTextField Label on hover悬停时的Objective-c下划线NSTextField标签
【发布时间】:2012-07-17 04:15:16
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c xcode macos cocoa


    【解决方案1】:

    你可能应该继承 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 文件中声明委托:

    @property (assign) IBOutlet id delegate;
    

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

    之后就可以实现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。 它通常是一个控制器(窗口控制器)。

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

    【讨论】:

    • 感谢您提供非常完整的答案。我做了一些问题,我应该把+(id)fakeHyperlinkFromString:(NSString*)inString withColor:(NSColor*)color {}放在哪里,我应该把它放在哪个文件中,MainWindowController.h还是MainWindowController.m?另外,我在上面写着myTextField 的地方放了什么,文本字段的名称?只是让你知道,我对 Objective-C 有点陌生,并且随着我的学习而学习,你能解释一下子视图吗?谢谢!
    • 我已经更新了答案。阅读有关类别的更多信息:developer.apple.com/library/mac/#documentation/General/…
    • 首先 - 你最好写 @implementation NSAttributedString (Hyperlink) 是一个名为 NSAttributedString+Hyperlink.m 的单独文件。无论如何,您确实包含了头文件,因此请利用优势;)
    • 第二 - 执行 alloc/init 时发生内存泄漏,然后将变量保留在自己的位置,下次再次分配内存。您应该将它分配给一个变量并在不需要时释放它,或者使用 autorelease 方法告诉 Autorelease Pool 稍后释放内存。
    • 第三 - 在 drawRect 方法中创建字符串是一个糟糕的设计恕我直言。创建 2 个名为 text_onetext_two 的属性(或私有类变量)并将字符串存储在其中。使用操作或委托调用来更新字符串值/选项(您可以将属性字符串分配给您的 TextField 的同一点)。然后让drawRect画出此刻存储的内容。
    猜你喜欢
    • 2013-05-26
    • 2014-01-18
    • 2014-08-30
    • 2021-01-22
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2014-04-12
    • 2018-02-17
    相关资源
    最近更新 更多