我想爲我的應用程序設置一個自定義鼠標光標,並且希望通過用自定義顏色替換白色邊框來以編程方式將默認光標的顏色更改爲自定義顏色。問題是,我甚至不知道從哪裏開始以編程方式編輯Cocoa中的圖像,所以任何幫助都是值得讚賞的!以編程方式編輯Cocoa中的圖像?
0
A
回答
0
我的最終代碼。這將採用標準IBeam光標(當您將鼠標懸停在文本視圖上時),並將指針中的彩色光標存儲在coloredIBeamCursor
指針中。
- (void)setPointerColor:(NSColor *)newColor {
// create the new cursor image
[[NSGraphicsContext currentContext] CIContext];
// create the layer with the same color as the text
CIFilter *backgroundGenerator=[CIFilter filterWithName:@"CIConstantColorGenerator"];
CIColor *color=[[[CIColor alloc] initWithColor:newColor] autorelease];
[backgroundGenerator setValue:color forKey:@"inputColor"];
CIImage *backgroundImage=[backgroundGenerator valueForKey:@"outputImage"];
// create the cursor image
CIImage *cursor=[CIImage imageWithData:[[[NSCursor IBeamCursor] image] TIFFRepresentation]];
CIFilter *filter=[CIFilter filterWithName:@"CIColorInvert"];
[filter setValue:cursor forKey:@"inputImage"];
CIImage *outputImage=[filter valueForKey:@"outputImage"];
// apply a multiply filter
filter=[CIFilter filterWithName:@"CIMultiplyCompositing"];
[filter setValue:backgroundImage forKey:@"inputImage"];
[filter setValue:outputImage forKey:@"inputBackgroundImage"];
outputImage=[filter valueForKey:@"outputImage"];
// get the NSImage from the CIImage
NSCIImageRep *rep=[NSCIImageRep imageRepWithCIImage:outputImage];
NSImage *newImage=[[[NSImage alloc] initWithSize:[outputImage extent].size] autorelease];
[newImage addRepresentation:rep];
// remove the old cursor (if any)
if (coloredIBeamCursor!=nil) {
[self removeCursorRect:[self visibleRect] cursor:coloredIBeamCursor];
[coloredIBeamCursor release];
}
// set the new cursor
NSCursor *coloredIBeamCursor=[[NSCursor alloc] initWithImage:newImage hotSpot:[[NSCursor IBeamCursor] hotSpot]];
[self resetCursorRects];
}
0
您可以使用 - [NSCursor arrowCursor]獲取默認光標。一旦你有了遊標,你就可以用 - [NSCursor圖像]獲得它的圖像。你不應該修改另一個對象的圖像,所以你應該複製該圖像。然後你應該編輯圖像,並用 - [NSCursor initWithImage:hotSpot:]創建一個新的遊標。你的代碼應該是這個樣子:
- (NSImage *)customArrowCursorImage {
NSImage *image = [[[NSCursor arrowCursor] image] copy];
[image lockFocus];
/// Do custom drawing
[image unlockFocus];
}
- (NSCursor *)customArrowCursor {
NSImage *image = [self customArrowCursorImage];
NSPoint hotSpot = [[NSCursor arrowCursor] hotSpot];
return [[[NSCursor alloc] initWithImage:image hotSpot:hotSpot] autorelease];
}
您應該能夠使用的核心圖像過濾器更換白色具有自定義圖像的顏色。但是,如果您只想開始使用,可以使用NSReadPixel()和NSRectFill一次爲一個像素着色。使用NSReadPixel和NSRectFill一次繪製一個像素的時間會非常慢,所以您只應該這樣做以瞭解所有這些工作方式。
相關問題
- 1. 以編程方式編輯Drupal節點中的圖像
- 2. 以編程方式編輯拼圖使用的圖像
- 3. 以編程方式在aloha編輯器中插入圖像
- 4. Cocoa - 以編程方式呈現NSViewController
- 5. Cocoa Touch - 以編程方式添加UIImageView?
- 6. 以編程方式編輯System.Servicemodel值?
- 7. 以編程方式編輯secpol.msc?
- 8. 以編程方式編輯flash cookies
- 9. SearchBar與searchDisplayController以編程方式編輯
- 10. 以編程方式編輯grub選項
- 11. 以編程方式編輯路徑
- 12. 以編程方式編輯Info.plist?
- 13. 以編程方式編輯EditText
- 14. 以編程方式編輯Python源碼
- 15. 以編程方式編輯IIS IPGrant表
- 16. 以編程方式編輯.webarchive文件
- 17. 如何以編程方式編輯GridView?
- 18. 以編程方式編輯NSLayoutConstraint常量?
- 19. 以編程方式編輯.conf文件
- 20. 以編程方式編輯Web.config
- 21. 如何以編程方式編輯datagridview?
- 22. 如何以編程方式編輯xorg.conf?
- 23. 在Cocoa/XCode中以編程方式添加快捷方式
- 24. 如何以編程方式將多個圖像添加到可編輯視圖
- 25. 以編程方式添加圖像
- 26. 以編程方式創建圖像?
- 27. 以編程方式設置ImageButton圖像
- 28. C#以編程方式形成圖像
- 29. 以編程方式更改圖像
- 30. 以編程方式更改圖像源
非常感謝你的回答,我終於知道了如何爲光標着色(請參閱下面的答案)。 – Nickkk 2011-06-25 10:15:03