// // GraphicViewView.swift // uiGrafik1 // // Created by Michael Wilhelm on 30/04/2016. // Copyright © 2016 Michael Wilhelm. All rights reserved. // import UIKit // https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGContext/index.html // http://techotopia.com/index.php/An_iOS_8_Swift_Graphics_Tutorial_using_Core_Graphics_and_Core_Image // https://www.raywenderlich.com/87899/make-simple-drawing-app-uikit-swift /* let colorSpace = CGColorSpaceCreateDeviceRGB() let components: [CGFloat] = [0.0, 0.0, 1.0, 1.0] let color = CGColorCreate(colorSpace, components) override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) let rectangle = CGRectMake(60,170,200,80) CGContextAddRect(context, rectangle) CGContextStrokePath(context) } override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) let rectangle = CGRectMake(60,170,200,80) CGContextAddEllipseInRect(context, rectangle) CGContextStrokePath(context) } fillRaute override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextMoveToPoint(context, 100, 100) CGContextAddLineToPoint(context, 150, 150) CGContextAddLineToPoint(context, 100, 200) CGContextAddLineToPoint(context, 50, 150) CGContextAddLineToPoint(context, 100, 100) CGContextSetFillColorWithColor(context, UIColor.redColor().CGColor) CGContextFillPath(context) } Rect with Border override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) let rectangle = CGRectMake(60,170,200,80) CGContextAddRect(context, rectangle) CGContextStrokePath(context) CGContextSetFillColorWithColor(context, UIColor.redColor().CGColor) CGContextFillRect(context, rectangle) } Bogen override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) CGContextMoveToPoint(context, 100, 100) CGContextAddArcToPoint(context, 100,200, 300,200, 100) CGContextStrokePath(context) } Bezier override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) CGContextMoveToPoint(context, 10, 10) CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400) CGContextStrokePath(context) } Bezuer override func drawRect(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) CGContextMoveToPoint(context, 10, 200) CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200) CGContextStrokePath(context) } Strichart let dashArray:[CGFloat] = [2,6,4,2] CGContextSetLineDash(context, 3, dashArray, 4) */ class GraphicViewView: UIView { func test1(rect: CGRect){ let context = UIGraphicsGetCurrentContext() let red = UIColor.redColor().CGColor CGContextSetLineWidth(context, 3.0) CGContextSetStrokeColorWithColor(context, red) CGContextMoveToPoint(context, 10, 10) CGContextAddLineToPoint(context, 200, 500) CGContextStrokePath(context) } func test2(rect: CGRect){ let context = UIGraphicsGetCurrentContext() let red = UIColor.redColor().CGColor let blue = UIColor.blueColor().CGColor CGContextSetLineWidth(context, 3.0) CGContextSetStrokeColorWithColor(context, red) CGContextMoveToPoint(context, 10, 10) CGContextAddLineToPoint(context, rect.width-10, rect.height-10) CGContextStrokePath(context) CGContextSetLineWidth(context, 1.0) CGContextSetStrokeColorWithColor(context, blue) CGContextMoveToPoint(context, rect.width-10, 10) CGContextAddLineToPoint(context, 10, rect.height-10) CGContextStrokePath(context) // CGContextSetTextDrawingMode(<#T##c: CGContext?##CGContext?#>, <#T##mode: CGTextDrawingMode##CGTextDrawingMode#>) var s: NSString = "ABCDEF" // set the text color to dark gray let fieldColor: UIColor = UIColor.darkGrayColor() // set the font to Helvetica Neue 18 let fieldFont = UIFont(name: "Helvetica Neue", size: 18) // set the line spacing to 6 var paraStyle = NSMutableParagraphStyle() paraStyle.lineSpacing = 6.0 // set the Obliqueness to 0.1 var skew = 0.1 let font = UIFont(name: "Palatino-Roman", size: 14.0) let baselineAdjust = 1.0 // NSDictionary //let dict = NSDictionary() // [NSFontAttributeName:font!, NSBaselineOffsetAttributeName:baselineAdjust] let attrsDictionary = [NSFontAttributeName:font!, NSBaselineOffsetAttributeName:baselineAdjust] CGContextSetStrokeColorWithColor(context, UIColor.greenColor().CGColor) s.drawInRect(CGRectMake(20.0, 140.0, 300.0, 48.0), withAttributes: attrsDictionary) CGContextStrokePath(context) } func test3(rect: CGRect) { let context = UIGraphicsGetCurrentContext() let locations: [CGFloat] = [ 0.0, 0.25, 0.5, 0.75 ] let colors = [UIColor.redColor().CGColor, UIColor.greenColor().CGColor, UIColor.blueColor().CGColor, UIColor.yellowColor().CGColor] let colorspace = CGColorSpaceCreateDeviceRGB() let gradient = CGGradientCreateWithColors(colorspace, colors, locations) var startPoint = CGPoint() var endPoint = CGPoint() startPoint.x = 0.0 startPoint.y = 0.0 endPoint.x = 600 endPoint.y = 600 // CGGradientDrawingOptions CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, CGGradientDrawingOptions.DrawsAfterEndLocation) } // Bogens func test4(rect:CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) CGContextMoveToPoint(context, 100, 100) CGContextAddArcToPoint(context, 100,200, 300,200, 100) CGContextStrokePath(context) } // Bezier func test5(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) CGContextMoveToPoint(context, 10, 10) CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400) CGContextStrokePath(context) } // Bezier func test6(rect: CGRect) { let context = UIGraphicsGetCurrentContext() CGContextSetLineWidth(context, 4.0) CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor) CGContextMoveToPoint(context, 10, 200) CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200) CGContextStrokePath(context) } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code test6(rect) } }