首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现苹果在Mac消息中的内嵌文本样式?

如何实现苹果在Mac消息中的内嵌文本样式?
EN

Stack Overflow用户
提问于 2012-04-15 01:12:02
回答 1查看 814关注 0票数 0

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

正如你所看到的,即使在浅灰色的背景上,文本也是“白色阴影”的。粗体文本确实有亚像素渲染,而灰色文本没有(根据设计?)。

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

那么,什么才是正确的方法呢?任何技巧,否则只能子类化NSTextFields

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-15 01:53:01

解决方案1:

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

解决方案2:

当然,这可以通过对NSTextFieldCell进行子类化并添加阴影来完成。

像这样的

代码语言:javascript
复制
- (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];
}

结果:

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10155603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档