gpt4 book ai didi

cocoa - 如何实现 Mac 版 Apple Messages 中的凹进文本样式?

转载 作者:行者123 更新时间:2023-12-03 17:30:22 25 4
gpt4 key购买 nike

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

example

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

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

那么,正确的方法是什么?有什么技巧或者只需要继承 NSTextFields 吗?

最佳答案

解决方案 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];
}

结果:

Text example

关于cocoa - 如何实现 Mac 版 Apple Messages 中的凹进文本样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10155603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com