Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:32:22+00:00 2026-06-14T17:32:22+00:00

Inside my application I’m programmatically constructing a NSImage object that I set as the

  • 0

Inside my application I’m programmatically constructing a NSImage object that I set as the applications dock Icon. I want to add some text to the icon and have been attempting to use NSString drawAtPoint: withAttributes but it doesn’t seem to be working. I’ve confirmed using log messages that the string is getting constructed properly.

I can’t seem to figure out what I’m missing, doing wrong. Any help would be greatly appreciated.

Here is the function I’ve written to draw to the NSImage

-(void) drawStringToImage:(NSString*) str{

    [theIcon lockFocus];

    NSLog([@"Drawing String: " stringByAppendingString:str]);
   //  [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    [str drawAtPoint:NSMakePoint(5,500) withAttributes:nil];

    [theIcon unlockFocus];
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:32:24+00:00Added an answer on June 14, 2026 at 5:32 pm

    Using modified code from How to convert Text to Image in Cocoa Objective-C I was able to render text on top of a existing NSImage

    // Use Helvetica size 200 
    CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica Bold"), 200.0, nil);
    
    // Setup the string attributes, set TEXT COLOR to WHITE
    NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                (__bridge id)(font), kCTFontAttributeName,
                                [[NSColor whiteColor] CGColor], (__bridge id)(kCTForegroundColorAttributeName),
                                nil];
    
    NSAttributedString* as = [[NSAttributedString alloc] initWithString:string attributes:attributes];
    CFRelease(font);
    
    // Calculate the size required to contain the Text
    CTLineRef textLine = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)as);
    CGFloat ascent, descent, leading;
    double fWidth = CTLineGetTypographicBounds(textLine, &ascent, &descent, &leading);
    size_t w = (size_t)ceilf(fWidth);
    size_t h = (size_t)ceilf(ascent + descent);
    
    //Allocated data for the image
    void* data = malloc(w*h*4);
    
    // Create the context and fill it with white background
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
    CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, w*4, space, bitmapInfo);
    CGColorSpaceRelease(space);
    CGContextSetRGBFillColor(ctx, 0.0, 0.0, 0.0, 0.0); // black background
    CGContextFillRect(ctx, CGRectMake(0.0, 0.0, w, h));
    
    // Draw the text in the new CoreGraphics Context
    CGContextSetTextPosition(ctx, 0.0, descent);
    CTLineDraw(textLine, ctx);
    CFRelease(textLine);
    
    // Save the CoreGraphics Context to a NSImage
    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
    NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage:imageRef];
    NSImage *stringImage = [[NSImage alloc] initWithSize:size];
    [stringImage addRepresentation:imageRep];
    
    // Combine the original image with the new Text Image
    [originalImage lockFocus];
    [stringImage drawInRect:NSMakeRect(renderArea.origin.x, renderArea.origin.y, w, h) fromRect:NSZeroRect operation:NSCompositeSourceAtop fraction:1];
    [originalImage unlockFocus];
    
    CGImageRelease(imageRef);
    free(data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to use the iPhones Mail.app inside my application so
Does anyone know of a library that enables you to run an application inside
Hi I want to create Menu inside Application. Means I want my application should
I want to use show function inside application didFinishLaunchingWithOptions -(void)show { NSLog(@AAA); } -
Working inside the context of an ASP.NET application I am creating a page that
I have developed an application inside ESRI ArcMAP that uses several forms that are
I want to Link my Application Settings(created using Application Preferences) from button inside Application,
I created an application inside IIS 7.5 and inside that application a virtual directory
I'm trying to make a way to disable some view helpers that are inside
I have icons inside my application for uibutton & on uitable cell. Lets say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.