2
A
回答
2
尝试creating an alpha mask from the text然后使用NSGradient绘制它。以下是一个基于链接代码的简单示例:
- (void)drawRect:(NSRect)rect
{
// Create a grayscale context for the mask
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CGContextRef maskContext =
CGBitmapContextCreate(
NULL,
self.bounds.size.width,
self.bounds.size.height,
8,
self.bounds.size.width,
colorspace,
0);
CGColorSpaceRelease(colorspace);
// Switch to the context for drawing
NSGraphicsContext *maskGraphicsContext =
[NSGraphicsContext
graphicsContextWithGraphicsPort:maskContext
flipped:NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:maskGraphicsContext];
// Draw the text right-way-up (non-flipped context)
[text
drawInRect:rect
withAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont fontWithName:@"HelveticaNeue-Bold" size:124], NSFontAttributeName,
[NSColor whiteColor], NSForegroundColorAttributeName,
nil]];
// Switch back to the window's context
[NSGraphicsContext restoreGraphicsState];
// Create an image mask from what we've drawn so far
CGImageRef alphaMask = CGBitmapContextCreateImage(maskContext);
// Draw a white background in the window
CGContextRef windowContext = [[NSGraphicsContext currentContext] graphicsPort];
[[NSColor whiteColor] setFill];
CGContextFillRect(windowContext, rect);
// Draw the gradient, clipped by the mask
CGContextSaveGState(windowContext);
CGContextClipToMask(windowContext, NSRectToCGRect(self.bounds), alphaMask);
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[NSColor blackColor] endingColor:[NSColor grayColor]];
[gradient drawInRect:rect angle:-90];
[gradient release];
CGContextRestoreGState(windowContext);
CGImageRelease(alphaMask);
}
这使用视图边界作为渐变边界;如果你想更准确,你需要得到文本高度(有关该信息here)。
相关问题
- 1. JFreeChart条形图 - 绘制没有渐变填充的绘图
- 2. 添加填充到带有可绘制背景的textView
- 3. 如何绘制略有对角线的渐变填充PHP?
- 4. 4路渐变填充。可能?
- 5. 渐变填充的内容HTML中的可编辑文本
- 6. Android TextView可绘制,更改可绘制和文本之间的填充?
- 7. 基R绘制渐变线,可能吗?
- 8. Android线性渐变可绘制
- 9. Android XML可绘制透明渐变
- 10. 绘制渐变填充的多边形中选择使用
- 11. 可绘制填充不起作用
- 12. 可可CoreGraphics:如何使用渐变填充多个形状
- 13. 画布绘制用线性渐变填充圆角矩形
- 14. 如何绘制渐变填充弧,即彩虹?
- 15. 绘制图像与圆角,边框和渐变填充C#
- 16. iOS,绘制径向渐变,填充四边形
- 17. Android:使用渐变填充颜色绘制弧线
- 18. 绘制渐变
- 19. 带有渐变填充的XML圆形Android
- 20. 具有渐变填充的NSView?
- 21. 是否可以对可绘制的渐变进行抖动?
- 22. jVectormap渐变背景填充
- 23. Quartz2d渐变填充椭圆
- 24. 用渐变填充geom_tile
- 25. 如何填充渐变
- 26. 矩形渐变填充
- 27. 样条图渐变填充
- 28. MPAndroidChart填充颜色渐变
- 29. 渐变不填充IBDesignable UIView
- 30. 如何在可可中绘制椭圆渐变?
谢谢!我还从Apple的示例代码(SpeedometerView)中找到了一个类别,用于将字符串转换为贝塞尔路径的NSString,其余部分仅改变路径并用渐变填充。 – koo 2010-12-06 03:25:24