// DrawingDemoView.swift Copyright (c) 2015 Kari Laitinen. All rights reserved. // 2015-08-18 File created. // 2015-10-17 Modifications for Xcode 7, Swift 2 // 2016-08-11 Last modification. /* This program demonstrates some basic drawing operations. */ import UIKit class DrawingDemoView: UIView { override func drawRect( rect: CGRect ) { let context: CGContextRef = UIGraphicsGetCurrentContext()! let view_width = self.bounds.size.width let view_height = self.bounds.size.height let text_to_draw = String( format: "View size is %.0f x %.0f", view_width, view_height ) let text_color: UIColor = UIColor.blackColor() // set the font to Palatino-Roman 14 let font = UIFont( name: "Palatino-Roman", size:14.0) // set the line spacing to 6 let paragraph_style = NSMutableParagraphStyle() paragraph_style.lineSpacing = 6.0 // set the Obliqueness to 0.1 let skew = 0.0 let text_attributes: NSDictionary = [ NSForegroundColorAttributeName: text_color, NSParagraphStyleAttributeName: paragraph_style, NSObliquenessAttributeName: skew, NSFontAttributeName: font! ] ( text_to_draw as NSString ).drawAtPoint( CGPointMake( 20.0, 40.0 ), withAttributes: text_attributes as? [String : AnyObject]) // The next two lines specify that we'll be stroking with // green color and making lines that are two pixels wide. CGContextSetLineWidth( context, 2.0 ) CGContextSetStrokeColorWithColor( context, UIColor.greenColor().CGColor ) // Next we specify a drawing path for a horizontalline CGContextBeginPath( context ) CGContextMoveToPoint( context, view_width / 2 - 120, view_height / 8 ) CGContextAddLineToPoint( context, view_width / 2 + 120, view_height / 8 ) CGContextStrokePath( context ) // This draws the line // Next we'll make a rectangle that is filled with cyan color. CGContextBeginPath( context ) CGContextSetFillColorWithColor(context, UIColor.cyanColor().CGColor ) let cyan_rectangle = CGRectMake( view_width / 2 - 80, view_height / 4 - 50, 160, 80 ) CGContextAddRect( context, cyan_rectangle ) CGContextFillPath( context ) // Let's start using blue as stroke color CGContextSetStrokeColorWithColor( context, UIColor.blueColor().CGColor ) // The following statement creates a stroked rectangle around the // cyan rectangle. CGContextStrokeRect( context, CGRectMake( view_width / 2 - 90, view_height / 4 - 60, 180, 100 ) ) // Next we'll draw a magenta 'ball' to the center of the view CGContextBeginPath( context ) CGContextSetFillColorWithColor( context, UIColor.magentaColor().CGColor ) let centered_square = CGRectMake( view_width / 2 - 80, view_height / 2 - 80, 160, 160 ) CGContextAddEllipseInRect( context, centered_square ) // The following call both fills and strokes the path with selected colors. CGContextDrawPath( context, CGPathDrawingMode.FillStroke) // Let's change the colors for filling and stroking. CGContextSetFillColorWithColor( context, UIColor.yellowColor().CGColor ) CGContextSetStrokeColorWithColor( context, UIColor.blackColor().CGColor ) CGContextSetLineWidth( context, 4.0 ) // Somewhat wider lines. // The following lines create a Pacman shape CGContextBeginPath( context ) CGContextMoveToPoint( context, view_width / 2 - 40, view_height / 5 * 4 ) // start from center CGContextAddArc(context, view_width / 2 - 40, // center point x view_height / 5 * 4, // center point y 80, // radius of the arc CGFloat( 0.25 * M_PI), // start angle in radians CGFloat( 1.75 * M_PI ), // end angle in radians 0 ) // create a counterclockwise arc CGContextClosePath( context ) CGContextDrawPath( context, CGPathDrawingMode.FillStroke) // Next we'll draw the 'missing pie' of the pacman. // The center point is now 80 points to the right of the center point // of the pacman. When the last parameter for the CGContextAddArc // function is 1, we'll make a clockwise arc. CGContextBeginPath( context ) CGContextMoveToPoint( context, view_width / 2 + 40, view_height / 5 * 4 ) // start from center CGContextAddArc(context, view_width / 2 + 40, view_height / 5 * 4, 80, CGFloat( 0.25 * M_PI), CGFloat( 1.75 * M_PI ), 1 ) CGContextClosePath( context ) CGContextDrawPath( context, CGPathDrawingMode.FillStroke ) } }