0

我想复制 Messages/iMessage 的嵌入式文本样式,或者浅灰色背景上的文本“白色阴影”样式。

例子

如您所见,即使在浅灰色背景上,文本也带有“白色阴影”。粗体文本确实有亚像素渲染,而灰色文本没有(设计?)。

我试过了-setBackgroundStyle:NSBackgroundStyleRaised。然而,它产生的阴影比背景更暗。-setBackgroundStyle:NSBackgroundStyleLowered更糟糕的是它甚至覆盖了我的字体颜色设置。

那么,这样做的正确方法是什么?有什么技巧还是只需要子类化NSTextFields

4

1 回答 1

5

解决方案1:

我能想到的最简单的解决方案是在彼此上写两个文本(例如,顶部为灰色,底部为白色,底部相差 1px)。


解决方案2:

当然,它可以通过子类化NSTextFieldCell和添加阴影来完成。

像这样:

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSShadow *shadow = [[NSShadow alloc] init];
    [shadow setShadowOffset:NSMakeSize(0,-1)];
    [shadow setShadowColor:[NSColor whiteColor]];
    [shadow setShadowBlurRadius:0];

    NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragStyle setAlignment:[self alignment]];

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                      [self font],NSFontAttributeName,
                      shadow,NSShadowAttributeName,
                      [self textColor],NSForegroundColorAttributeName,
                      paragStyle,NSParagraphStyleAttributeName,nil];
    [shadow release];
    [paragStyle release];

    NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
    [self setAttributedStringValue:string];
    [string release];
    [[self attributedStringValue] drawInRect:cellFrame];
}

结果:

文本示例

于 2012-04-14T17:53:01.443 回答