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