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

正如你所看到的,即使在浅灰色的背景上,文本也是“白色阴影”的。粗体文本确实有亚像素渲染,而灰色文本没有(根据设计?)。
我试过-setBackgroundStyle:NSBackgroundStyleRaised。然而,它产生的阴影比背景更暗。更糟糕的是,-setBackgroundStyle:NSBackgroundStyleLowered甚至覆盖了我的字体颜色设置。
那么,什么才是正确的方法呢?任何技巧,否则只能子类化NSTextFields
发布于 2012-04-15 01:53:01
解决方案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];
}结果:

https://stackoverflow.com/questions/10155603
复制相似问题