如何在 Cocoa 中使用图像作为文本字段的背景?

发布于 2024-07-17 12:35:43 字数 45 浏览 2 评论 0原文

是否可以使用图像作为 Cocoa 中文本字段的背景? 如果是这样,怎么办?

Is it possible to use an image as a background for a Text Field in Cocoa?
If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最笨的告白 2024-07-24 12:35:43

我不知道这是否是“正确”的方法,但首先想到的是创建 NSTextField 的自定义子类,它可能大致如下所示:

- (void)awakeFromNib
{
    [self setDrawsBackground:NO];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];

    [self lockFocus];
    [[NSImage imageNamed:@"<#image filename#>"] drawInRect:rect
        fromRect:rect
        operation:NSCompositeSourceOver
        fraction:1.0];
    [self unlockFocus];
}

同样,这只是一个粗略的轮廓必要的部分。

无论如何,就像我说的,我不确定这是否真的是“正确”的方法(或者是否有“正确”的方法,就此而言),但这将为您的 NSTextField 提供背景图像。

根据 Joshua 的评论进行编辑(我不会在那个小小的评论框中有足够的空间):

要将图像添加到您的项目中,您可以将其从任何位置拖动到项目窗口(项目窗口中间的主要文件列表,尽管根据您设置 Xcode 编辑环境的方式,这对您来说可能会有所不同)。

为了子类化 NSTextField,您需要创建一个新的 Objective-C 类文件(文件 -> 新文件...),但编辑标头以便该类继承自 NSTextField< /code> 而不是 NSObject。 换句话说,您的头文件可能如下所示:

#import <Cocoa/Cocoa.h>

@interface BGImageTextField : NSTextField
{
}

@end

至于代码的其余部分,您需要将其添加到实现文件的主体中(例如,BGImageTextField.m),特别是在 @implementation@end 关键字之间。

我还想提两件事。 首先,我建议您阅读 Aaron Hillegass 所著的 Mac OS X 的 Cocoa 编程,它涵盖了我刚刚学习过的大部分 Cocoa 基础知识,并且是学习 Cocoa 的最佳方法之一。总体学习可可。 其次,虽然我的方法有效,但它可能不是最好的方法 - 特别是因为我最近刚刚发现 这篇文章,这似乎暗示了扩展 NSTextField 的更好方法。

I don't know if this is the "correct" way to do it, but the first thing that comes to mind would be to make a custom subclass of NSTextField, which might look roughly like this:

- (void)awakeFromNib
{
    [self setDrawsBackground:NO];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];

    [self lockFocus];
    [[NSImage imageNamed:@"<#image filename#>"] drawInRect:rect
        fromRect:rect
        operation:NSCompositeSourceOver
        fraction:1.0];
    [self unlockFocus];
}

Again, that's a just rough outline of the essential parts.

Anyways, like I said, I'm not sure if this really the "correct" way to do it (or if there is even a "correct" way, for that matter), but this will give you a background image for your NSTextField.

Edit in response to Joshua's comment (I'm not going to have enough room in that tiny little comment box):

To add the image into your project, you'd drag it from wherever it is into the project window (the main list of files in the middle of the project window, although depending on how you've set up your Xcode editing environment, this might be different for you).

In order to subclass NSTextField, you would want to create a new Objective-C class file (File -> New File…), but edit the header so that the class inherits from NSTextField instead of NSObject. In other words, your header file might look like this:

#import <Cocoa/Cocoa.h>

@interface BGImageTextField : NSTextField
{
}

@end

As for the rest of the code, you would want to add that in the main body of the implementation file (BGImageTextField.m, for example), specifically in between the @implementation and @end keywords.

I'd also like to mention two things. First, I'd recommend picking up a copy of Cocoa Programming for Mac OS X, by Aaron Hillegass—it covers most of the Cocoa basics that I just went over, and is one of the best ways to learn Cocoa in general. Secondly, although my approach works, it's probably not the best approach—especially since I just recently found this post, which seems to hint at a better way of extending NSTextField.

浮云落日 2024-07-24 12:35:43

不要子类化 NSTextField,只需将背景颜色设置为透明 [NSColor clearColor] 并将图像放在其后面即可。

Instead of subclassing NSTextField, just make the background color transparent [NSColor clearColor] and put the image behind it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文