如何实现Apple的Messages for Mac中的隐藏文本样式?

0 cocoa nstextfield nstextfieldcell

我想复制Messages/iMessage的隐藏文本样式,或者浅灰色背景上的文本"白色阴影"样式.

例

正如您所看到的,即使在浅灰色背景下,文本也带有"白色阴影".粗体文本确实有子像素渲染,而灰色文本没有(按设计?).

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

那么,这样做的正确方法是什么?任何技巧或只需要子类NSTextFields

Jus*_*Boo 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];
}
Run Code Online (Sandbox Code Playgroud)

结果:

文字示例