Home > Back-end >  Looking for simple example to write text into a NSImage using Cocoa
Looking for simple example to write text into a NSImage using Cocoa

Time:05-10

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

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

I am looking for a way to simply write a String onto this image.... and coming up very short. This does not worker me.

CodePudding user response:

The following code will place a mutable attributed string on an 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];
  • Related