簡體   English   中英

尋找使用 Cocoa 將文本寫入 NSImage 的簡單示例

[英]Looking for simple example to write text into a NSImage using Cocoa

    NSImage *myNewIconImage=[[NSImage imageNamed:@"FanImage"] copy];
    [myNewIconImage lockFocus];
    [@"15" drawAtPoint:NSZeroPoint withAttributes:nil];
    [myNewIconImage unlockFocus];

    [myNewIconImage setTemplate:YES];
    [[NSApplication sharedApplication]] setApplicationIconImage:myNewIconImage];
    

我正在尋找一種方法來簡單地在這張圖片上寫一個字符串……而且很短。 這對我不起作用。

以下代碼將在 NSImage 上放置一個可變的屬性字符串:

NSImageView *imageView = [[NSImageView alloc] initWithFrame:NSMakeRect( 0, 0, _wndW, _wndH )]; 
[[window contentView] addSubview:imageView];
NSImage *image = [NSImage imageNamed:@"myImage.jpg"];
[image lockFocus];
NSMutableDictionary *attr = [NSMutableDictionary dictionary];
[attr setObject:[NSFont fontWithName:@"Lucida Grande" size:36] forKey:NSFontAttributeName];
[attr setObject: [NSNumber numberWithFloat: 10.0] forKey: NSStrokeWidthAttributeName];
[attr setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
NSString *myStr = @"Welcome to Cocoa";
NSMutableAttributedString *s = [[NSMutableAttributedString alloc] initWithString: myStr attributes:attr];
[s drawAtPoint:NSMakePoint(130,130)];
[image unlockFocus];
[imageView setImage:image];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM