如何在 NSTextField 中放置内部阴影?

Le_*_*eur 2 cocoa

我需要在 NSTextField 中为文本添加内阴影,有什么解决方案吗?

Meg*_*ing 5

NSTextField 是一个 NSView 的子类,它有一个 shadow 字段,如果你创建了一个 shadow 对象并赋值给这个字段,view 在绘制的时候会自动显示一个阴影

    NSShadow* shadow = [[NSShadow alloc] init];
    shadow.shadowBlurRadius = 2; //set how many pixels the shadow has
    shadow.shadowOffset = NSMakeSize(2, -2); //the distance from the text the shadow is dropped
    shadow.shadowColor = [NSColor blackColor];
    self.textfield.shadow = shadow;
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为在 drawRect 上绘制的所有视图都通过使用 [shadow set] 使用此阴影属性。

在绘制操作期间执行 [shadow set] 会使之后绘制的任何内容都复制到下面