首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NSSearchField上添加阴影

如何在NSSearchField上添加阴影
EN

Stack Overflow用户
提问于 2012-12-27 10:05:05
回答 1查看 428关注 0票数 0

我想重写NSSearchField类,让它看起来像

我看了苹果的文档,发现NSSearchField是从NSTextField继承的,而NSControl是继承的,而NSControl本身是从NSView继承的。

因此,方法可以对应于setShadow: NSTextField,然而,我尝试在NSSearchField实例上设置一个方法,但实际上什么也没有发生。

有谁能告诉我如何获得阴影效果吗?谢谢~

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-27 14:49:35

使用NSShadow的NSTextField

代码语言:javascript
复制
// Modify theTextField so that its NSShadow will be visible.
theTextField.wantsLayer = YES ;
theTextField.bezeled = NO ;
theTextField.drawsBackground = NO ;

NSShadow* redShadow = [NSShadow new] ;
redShadow.shadowOffset = NSMakeSize(2, 2) ;
redShadow.shadowColor = [NSColor redColor] ;
theTextField.shadow = redShadow ;

这将导致:

在我使用NSShadows和NSTextFields/NSSearchFields的经验中,阴影不会出现,除非NSTextField没有边框并且不绘制其背景,并且闪烁的光标与它前面的文本一起阴影。

编辑:

重写drawRect:的子类NSSearchField

代码语言:javascript
复制
- (void) drawRect:(NSRect)dirtyRect {
    NSShadow* redShadow = [NSShadow new] ;
    redShadow.shadowOffset = NSMakeSize(2, -2) ;
    redShadow.shadowColor = [NSColor redColor] ;

    [NSGraphicsContext saveGraphicsState] ;
    self.wantsLayer = YES ;     // or NO
    [redShadow set] ;
    [super drawRect:dirtyRect] ;
    [NSGraphicsContext restoreGraphicsState] ;
}

这将导致:

。我假设你不希望放大镜图标或X按钮有阴影,所以你可以:

在原始文件后面添加第二个NSSearchField

在接口生成器中可能更容易做到这一点,但下面是在NSSearchField子类中实现这一点的代码。

代码语言:javascript
复制
- (void) awakeFromNib {
    [super awakeFromNib] ;

    NSSearchField* shadowSearchField = [NSSearchField new] ;
    [self.superview addSubview:shadowSearchField  positioned:NSWindowBelow  relativeTo:self ] ;
    shadowSearchField.translatesAutoresizingMaskIntoConstraints = NO ;
    shadowSearchField.editable = NO ;

    float horizontalOffset = -2 ;
    float verticalOffset   = -2 ;
    [self.superview addConstraint: [NSLayoutConstraint constraintWithItem:self  attribute:NSLayoutAttributeLeading  relatedBy:NSLayoutRelationEqual  toItem:shadowSearchField  attribute:NSLayoutAttributeLeading  multiplier:1  constant:horizontalOffset ] ] ;
    [self.superview addConstraint: [NSLayoutConstraint constraintWithItem:self  attribute:NSLayoutAttributeTop      relatedBy:NSLayoutRelationEqual  toItem:shadowSearchField  attribute:NSLayoutAttributeTop      multiplier:1  constant:verticalOffset ] ] ;
    [self.superview addConstraint: [NSLayoutConstraint constraintWithItem:self  attribute:NSLayoutAttributeWidth    relatedBy:NSLayoutRelationEqual  toItem:shadowSearchField  attribute:NSLayoutAttributeWidth    multiplier:1  constant:0 ] ] ;
    [self.superview addConstraint: [NSLayoutConstraint constraintWithItem:self  attribute:NSLayoutAttributeHeight   relatedBy:NSLayoutRelationEqual  toItem:shadowSearchField  attribute:NSLayoutAttributeHeight   multiplier:1  constant:0 ] ] ;
}

这将导致:

,如果你可以调整第二个NSSearchField的位置和颜色,它看起来最接近你想要的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14048539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档