gpt4 book ai didi

objective-c - Cocoa:如何像在 Mail.app 中一样绘制插入文本?

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:45 25 4
gpt4 key购买 nike

enter image description here

如何在 Cocoa (OS X) 中绘制这种文本样式?它似乎被用在几个 Apple 应用程序中,包括邮件(如上图所示)和 Xcode 侧边栏中的几个地方。我环顾四周,但未能找到任何建议如何重现这种特定风格的文本的资源。它看起来像一个嵌入的阴影,我的第一个猜测是尝试使用 NSShadow 并将模糊半径设置为负值,但显然只允许使用正值。有什么想法吗?

最佳答案

我有一些代码可以绘制浮雕单元格(我相信最初是 Jonathon Mah 写的)。它可能无法完全满足您的要求,但它会给您一个起点:

@implementation DMEmbossedTextFieldCell

#pragma mark NSCell

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
{
/* This method copies the three-layer method used by Safari's error page. That's accessible by forcing an
* error (e.g. visiting <foo://>) and opening the web inspector. */

// I tried to use NSBaselineOffsetAttributeName instead of shifting the frame, but that didn't seem to work
const NSRect onePixelUpFrame = NSOffsetRect(cellFrame, 0.0, [NSGraphicsContext currentContext].isFlipped ? -1.0 : 1.0);
const NSRange fullRange = NSMakeRange(0, self.attributedStringValue.length);

NSMutableAttributedString *scratchString = [self.attributedStringValue mutableCopy];

BOOL overDark = (self.backgroundStyle == NSBackgroundStyleDark);
CGFloat (^whenLight)(CGFloat) = ^(CGFloat b) { return overDark ? 1.0 - b : b; };

// Layer 1
[scratchString addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithCalibratedWhite:whenLight(1.0) alpha:1.0] range:fullRange];
[scratchString drawInRect:cellFrame];

// Layer 2
BOOL useGradient = NO; // Safari 5.2 preview has switched to a lighter, solid color look for the detail text. Since we use the same class, use bold-ness to decide
if (self.attributedStringValue.length > 0) {
NSFont *font = [self.attributedStringValue attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
if ([[NSFontManager sharedFontManager] traitsOfFont:font] & NSBoldFontMask)
useGradient = YES;
}

NSColor *solidShade = [NSColor colorWithCalibratedHue:200/360.0 saturation:0.03 brightness:whenLight(0.41) alpha:1.0];
[scratchString addAttribute:NSForegroundColorAttributeName value:solidShade range:fullRange];
[scratchString drawInRect:onePixelUpFrame];

// Layer 3 (Safari uses alpha of 0.25)
[scratchString addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithCalibratedWhite:whenLight(1.0) alpha:0.25] range:fullRange];
[scratchString drawInRect:cellFrame];
}

@end

关于objective-c - Cocoa:如何像在 Mail.app 中一样绘制插入文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20928743/

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