Añade un círculo rojo con un texto en blanco al icono de la aplicación en el dock

/ Published in: Objective C
Save to your folder(s)

El texto en blanco viene definido por el parámetro theNumber.
La posición del texto viene definido por el parámetro theRect

Copy this code and paste it in your HTML
  1. -(void) updateApplicationIcon:(int) theNumber:(NSRect) theRect{
  2. // get the current app icon
  3. NSImage *appImage = [NSImage imageNamed:@"NSApplicationIcon"];
  5. // create a new image and draw the app icon onto it
  6. NSImage *image = [[NSImage alloc] initWithSize:[appImage size]];
  8. // lock focus on the new image and draw the app icon onto it.
  9. // you'll want the image flipped so text shows up correctly
  10. [image setFlipped:TRUE];
  11. [image lockFocus];
  12. NSSize appSize = [appImage size];
  13. [appImage compositeToPoint:NSMakePoint(0, appSize.height)
  14. operation:NSCompositeSourceOver];
  16. // draw red circle
  17. NSBezierPath *redCircle = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(8,43,45,45)];
  18. [[NSColor redColor] setFill];
  19. [redCircle fill];
  21. // draw whatever else you want onto the image
  22. NSArray *values = [NSArray arrayWithObjects:[NSColor whiteColor], [NSFont fontWithName:@"Tahoma" size:32], nil];
  23. NSArray *keys = [NSArray arrayWithObjects:NSForegroundColorAttributeName, NSFontAttributeName, nil];
  25. NSDictionary *attrs = [NSDictionary dictionaryWithObjects:values forKeys:keys];
  26. NSString *caption= [NSString stringWithFormat:@"%d", theNumber];
  27. NSAttributedString *text= [[NSAttributedString alloc] initWithString:caption attributes:attrs];
  28. [text drawInRect:theRect];
  30. // unlock focus and set the new image as the dock icon
  31. [image unlockFocus];
  32. [NSApp setApplicationIconImage:image];
  33. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.