NSTextField 带阴影?
我希望在 Interface Builder 中设置的 NSTextFields 具有阴影。我已经实现了一种似乎有效的方法,但我不确定这是否是正确的方法。
我所做的是如下所示对 NSTextFieldCell 进行子类化,然后将我的子类设置为 IB 中 NSTextField 的单元格类型。这种做法有问题吗?有更好的办法吗?
#import "ShadowTextFieldCell.h"
static NSShadow *kShadow = nil;
@implementation ShadowTextFieldCell
+ (void)initialize
{
kShadow = [[NSShadow alloc] init];
[kShadow setShadowColor:[NSColor colorWithCalibratedWhite:0.f alpha:0.08f]];
[kShadow setShadowBlurRadius:0.f];
[kShadow setShadowOffset:NSMakeSize(0.f, -2.f)];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[kShadow set];
[super drawInteriorWithFrame:cellFrame inView:controlView];
}
@end
I'd like the NSTextFields I set up in Interface Builder to have shadows. I've implemented a way to do this which seems to work, but I'm not sure if it's the right way.
What I did is subclass NSTextFieldCell as follows and then set my subclass as the NSTextField's cell's type in IB. Is there a problem with this approach? Is there a better way?
#import "ShadowTextFieldCell.h"
static NSShadow *kShadow = nil;
@implementation ShadowTextFieldCell
+ (void)initialize
{
kShadow = [[NSShadow alloc] init];
[kShadow setShadowColor:[NSColor colorWithCalibratedWhite:0.f alpha:0.08f]];
[kShadow setShadowBlurRadius:0.f];
[kShadow setShadowOffset:NSMakeSize(0.f, -2.f)];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[kShadow set];
[super drawInteriorWithFrame:cellFrame inView:controlView];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以只使用 NSCell 的
setBackgroundStyle:
请参阅这个类似的问题;
Rather than subclass, you can just use NSCell's
setBackgroundStyle:
See this similar question;
这种方法没有什么问题。另一种选择是对文本字段进行分层(调用
[textField setWantsLayer:YES]
并使用 CALayer 的阴影属性,但这通常是一种不受欢迎的方法,因为 Core Animation 的文本渲染缺乏子像素抗锯齿功能。There's nothing wrong with this approach. The other option would be to layer-back your text field (call
[textField setWantsLayer:YES]
and use CALayer's shadow properties, but this is often an undesirable way to do it because Core Animation's text rendering lacks subpixel antialiasing.最简单的方法
就像 zpasternack 所说的那样,但这仅用于默认阴影,用于自定义一个子类或使用图层...
Easiest way is
just like zpasternack said, but this is used only for default shadow-ing, for custom one subclass or use the layer...