We Recommend

Programming in Objective-C Programming in Objective-C
Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming. The book makes no assumption about prior experience with object-oriented programming languages or with the C language (upon which Objective-C is based). And because of this, both novice and experienced programmers alike can use this book to quickly and effectively learn the fundamentals of Objective-C.


Posted By

xaviaracil on 06/29/06


Tagged

cocoa dock badge


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

nunofgs


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


Published in: Objective C 


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


  1. -(void) updateApplicationIcon:(int) theNumber:(NSRect) theRect{
  2. // get the current app icon
  3. NSImage *appImage = [NSImage imageNamed:@"NSApplicationIcon"];
  4.  
  5. // create a new image and draw the app icon onto it
  6. NSImage *image = [[NSImage alloc] initWithSize:[appImage size]];
  7.  
  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];
  15.  
  16. // draw red circle
  17. NSBezierPath *redCircle = [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(8,43,45,45)];
  18. [[NSColor redColor] setFill];
  19. [redCircle fill];
  20.  
  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];
  24.  
  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];
  29.  
  30. // unlock focus and set the new image as the dock icon
  31. [image unlockFocus];
  32. [NSApp setApplicationIconImage:image];
  33. }

Report this snippet 

You need to login to post a comment.