code_junkie

Create a colored bubble/circle programmatically in ObjectiveC and Cocoa

Nov 12th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. NSRect rect = NSMakeRect(10, 10, 10, 10);
  2. NSBezierPath* circlePath = [NSBezierPath bezierPath];
  3. [circlePath appendBezierPathWithOvalInRect: rect];
  4.  
  5. [[NSColor blackColor] setStroke];
  6. [[NSColor redColor] setFill];
  7.  
  8. [path stroke];
  9. [path fill];
  10.  
  11. - (void)drawRect:(NSRect)rect
  12. {
  13. // Get the graphics context that we are currently executing under
  14. NSGraphicsContext* gc = [NSGraphicsContext currentContext];
  15.  
  16. // Save the current graphics context settings
  17. [gc saveGraphicsState];
  18.  
  19. // Set the color in the current graphics context for future draw operations
  20. [[NSColor blackColor] setStroke];
  21. [[NSColor redColor] setFill];
  22.  
  23. // Create our circle path
  24. NSRect rect = NSMakeRect(10, 10, 10, 10);
  25. NSBezierPath* circlePath = [NSBezierPath bezierPath];
  26. [circlePath appendBezierPathWithOvalInRect: rect];
  27.  
  28. // Outline and fill the path
  29. [circlePath stroke];
  30. [circlePath fill];
  31.  
  32. // Restore the context to what it was before we messed with it
  33. [gc restoreGraphicsState];
  34. }
  35.  
  36. CGContextRef c = UIGraphicsGetCurrentContext();
  37. CGContextSetRGBFillColor(c, 40, 0, 255, 0.1);
  38. CGContextSetRGBStrokeColor(c, 0, 40, 255, 0.5);
  39.  
  40. // Draw a green solid circle
  41. CGContextSetRGBFillColor(c, 0, 255, 0, 1);
  42. CGContextFillEllipseInRect(c, CGRectMake(100, 100, 25, 25));
Add Comment
Please, Sign In to add comment