-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawingViewLetters.swift
155 lines (108 loc) · 4.24 KB
/
DrawingViewLetters.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// DrawingViewLetters.swift
// Integrated test v1
//
// Created by School on 7/15/15.
// Copyright (c) 2015 sunspot. All rights reserved.
//
import Foundation
import UIKit
var letters = Letters()
class DrawingViewLetters: UIView {
private var path = UIBezierPath()
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
print("Initializing")
}
required init?(coder aDecoder: NSCoder) {
//fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
setupView()
}
func setupView() {
backgroundColor = UIColor.whiteColor()
path.lineWidth = 5
path.lineCapStyle = CGLineCap.Round
}
override func drawRect(rect: CGRect) {
//println("in drawRect")
for letter in letters.letterlist{
drawLetters(letter, color:UIColor(hue: 0.66, saturation: 0.9, brightness: 1.0, alpha: 1.0))
}
UIColor.blackColor().set()
opaque = false
backgroundColor = nil
path.stroke()
}
func drawResultBackground(){
//println("in drawRect")
for letter in letters.letterlist{
drawLetters(letter, color:letter.3)
}
UIColor.blackColor().set()
opaque = false
backgroundColor = nil
path.stroke()
}
func reset() {
print("In reset")
path.removeAllPoints()
letters.randomize()
setNeedsDisplay()
}
// Don't use the color in letter, use the color given
func drawLetters(letter:(Int, Int, String, UIColor), color:UIColor){
let (x, y, name, _) = letter
//println("Bubble \(bubble)")
let context = UIGraphicsGetCurrentContext();
// Set the circle outerline-width
CGContextSetLineWidth(context, 3.0);
// Set the circle outerline-colour
color.set()
// Draw
CGContextStrokePath(context);
// Now for the text
// Flip the context coordinates, in iOS only.
//CGContextTranslateCTM(context, 0, self.bounds.size.height);
//CGContextScaleCTM(context, 1.0, -1.0);
let f: CGFloat = CGFloat(fontLetters)
let aFont = UIFont(name: "Optima-Bold", size: f)
// create a dictionary of attributes to be applied to the string
let attr:CFDictionaryRef = [NSFontAttributeName:aFont!,NSForegroundColorAttributeName:color]
// create the attributed string
let text = CFAttributedStringCreate(nil, name, attr)
// create the line of text
let line = CTLineCreateWithAttributedString(text)
CGContextSetTextMatrix(context, CGAffineTransformMake(CGFloat(1), CGFloat(0), CGFloat(0), CGFloat(-1),CGFloat(0), CGFloat(0)))
CGContextSetTextPosition(context, CGFloat(x-8), CGFloat(y+5))
CTLineDraw(line, context!)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//println("Touch Begin")
let touch = touches.first! as UITouch
path.moveToPoint(touch.locationInView(self))
setNeedsDisplay()
letters.inNewLetter(touch.locationInView(self).x, y:touch.locationInView(self).y)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
//println("Touch moved")
let touch = touches.first! as UITouch
path.addLineToPoint(touch.locationInView(self))
setNeedsDisplay()
letters.inNewLetter(touch.locationInView(self).x, y:touch.locationInView(self).y)
}
/*
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
//println("Touch moved")
var touch = touches.anyObject() as UITouch
path.addLineToPoint(touch.locationInView(self))
setNeedsDisplay()
bubbles.inNewBubble(touch.locationInView(self).x, y:touch.locationInView(self).y)
}
*/
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
//println("Touch Ended")
//let touch = touches.first! as UITouch
}
}