-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabelView.js
117 lines (102 loc) · 3 KB
/
LabelView.js
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
function LabelView () {
this.font = "20px Helvetica";
};
LabelView.prototype = new View();
LabelView.prototype.drawAtPosition = function(ctx, x, y) {
View.prototype.drawAtPosition.call(this, ctx, x, y);
if (this.hidden) {
return;
}
y += 20;
if (this.text) {
ctx.font = this.font;
ctx.fillStyle = this.textColor ? this.textColor : 'rgba(0, 0, 0, 1)';
if (this.lineWrap && this.frame.width > 0 && this.lineHeight) {
var newlineChunks = this.text.split('\n');
for (var j = 0; j < newlineChunks.length; j++) {
var paragraph = newlineChunks[j];
if (paragraph === "") {
y += this.lineHeight;
if (this.maxTextHeight && y > this.maxTextHeight) {
return;
}
continue;
}
var chunks = paragraph.split(' ');
var currentLine = '';
for(var i = 0; i < chunks.length; i++) {
var testLine = currentLine + chunks[i] + ' ';
var metrics = ctx.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > this.frame.width && i > 0) {
ctx.fillText(currentLine, x, y);
currentLine = chunks[i] + ' ';
y += this.lineHeight;
if (this.maxTextHeight && y > this.maxTextHeight) {
return;
}
} else {
currentLine = testLine;
if (currentLine === " " || currentLine === " ") {
currentLine = "";
}
}
}
ctx.fillText(currentLine, x, y);
y += this.lineHeight;
if (this.maxTextHeight && y > this.maxTextHeight) {
return;
}
}
} else {
if (!this.centerHorizontally) {
ctx.fillText(this.text, x, y);
} else {
var xPos = x + (this.frame.width - ctx.measureText(this.text).width) / 2;
ctx.fillText(this.text, xPos, y);
}
}
}
}
LabelView.prototype.height = function() {
return this.lineHeight * this.numberOfLines();
}
LabelView.prototype.width = function() {
globalCtx.font = this.font;
return globalCtx.measureText(this.text).width;
}
LabelView.prototype.numberOfLines = function() {
if (!this.lineWrap) {
return this.lineHeight;
}
globalCtx.save();
globalCtx.font = this.font;
var numLines = 0;
var newlineChunks = this.text.split('\n');
for (var x = 0; x < newlineChunks.length; x++) {
var paragraph = newlineChunks[x];
if (paragraph === "") {
numLines++;
continue;
}
var chunks = paragraph.split(' ');
var currentLine = '';
for(var i = 0; i < chunks.length; i++) {
var testLine = currentLine + chunks[i] + ' ';
var metrics = globalCtx.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > this.frame.width && i > 0) {
currentLine = chunks[i] + ' ';
numLines++;
} else {
currentLine = testLine;
if (currentLine === " " || currentLine === " ") {
currentLine = "";
}
}
}
numLines++;
}
globalCtx.restore();
return numLines;
}