-
Notifications
You must be signed in to change notification settings - Fork 1
/
framework.ui.html
469 lines (401 loc) · 18.6 KB
/
framework.ui.html
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!DOCTYPE html>
<html dir="ltr" class="js desktop" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<title>framework.ui</title>
<script>
(function(H) {
H.className = H.className.replace(/\bno-js\b/, 'js')
})(document.documentElement)
</script>
<link rel="stylesheet" type="text/css" href="css.css">
<script type="text/javascript" charset="utf-8" src="js.js"></script>
</head>
<body>
<!--[if lte IE 7 ]><div id="IE7"><![endif]-->
<!--[if IE 8 ]><div id="IE8"><![endif]-->
<div id="dokuwiki__site">
<div id="dokuwiki__top" class="dokuwiki site mode_show ">
<!-- ********** HEADER ********** -->
<div id="dokuwiki__header">
<div class="pad group">
<hr class="a11y">
</div>
</div>
<!-- /header -->
<div class="wrapper group">
<!-- ********** CONTENT ********** -->
<div id="dokuwiki__content"><div class="pad group">
<div class="pageId"><span>zh_cn:api:framework.ui</span></div>
<div class="page group">
<!-- wikipage start -->
<!-- TOC START -->
<div id="dw__toc">
<h3 class="toggle">Table of Contents</h3>
<div>
<ul class="toc">
<li class="level1"><div class="li"><a href="#frameworkui">framework.ui</a></div>
<ul class="toc">
<li class="level2"><div class="li"><a href="#uineweditbox">ui.newEditBox</a></div></li>
<li class="level2"><div class="li"><a href="#uinewimagemenuitem">ui.newImageMenuItem</a></div></li>
<li class="level2"><div class="li"><a href="#uinewttflabelmenuitem">ui.newTTFLabelMenuItem</a></div></li>
<li class="level2"><div class="li"><a href="#uinewmenu">ui.newMenu</a></div></li>
<li class="level2"><div class="li"><a href="#uinewbmfontlabel">ui.newBMFontLabel</a></div></li>
<li class="level2"><div class="li"><a href="#uinewttflabel">ui.newTTFLabel</a></div></li>
<li class="level2"><div class="li"><a href="#uinewttflabelwithshadow">ui.newTTFLabelWithShadow</a></div></li>
<li class="level2"><div class="li"><a href="#uinewttflabelwithoutline">ui.newTTFLabelWithOutline</a></div></li>
</ul></li>
</ul>
</div>
</div>
<!-- TOC END -->
<h1 class="sectionedit1" id="frameworkui">framework.ui</h1>
<div class="level1">
<p>
创建和管理用户界面
</p>
<p>
~~
</p>
</div>
<h2 class="sectionedit2" id="uineweditbox">ui.newEditBox</h2>
<div class="level2">
<p>
创建一个文字输入框,并返回 CCEditBox 对象。
</p>
<p>
格式:
</p>
<pre class="code lua">editbox <span class="sy0">=</span> ui<span class="sy0">.</span>newEditBox<span class="br0">(</span>参数表格对象<span class="br0">)</span></pre>
<p>
可用参数:
</p>
<ul>
<li class="level1"><div class="li">image: 输入框的图像,可以是图像名或者是 CCSprite9Scale 显示对象。用 display.newScale9Sprite() 创建 CCSprite9Scale 显示对象。</div>
</li>
<li class="level1"><div class="li">imagePressed: 输入状态时输入框显示的图像(可选)</div>
</li>
<li class="level1"><div class="li">imageDisabled: 禁止状态时输入框显示的图像(可选)</div>
</li>
<li class="level1"><div class="li">listener: 回调函数</div>
</li>
<li class="level1"><div class="li">size: 输入框的尺寸,用 CCSize(宽度, 高度) 创建</div>
</li>
<li class="level1"><div class="li">x, y: 坐标(可选)</div>
</li>
</ul>
<p>
用法示例:
</p>
<pre class="code lua"><span class="kw1">local</span> <span class="kw1">function</span> onEdit<span class="br0">(</span>event<span class="sy0">,</span> editbox<span class="br0">)</span>
<span class="kw1">if</span> event <span class="sy0">==</span> <span class="st0">"began"</span> <span class="kw1">then</span>
<span class="co1">-- 开始输入</span>
<span class="kw1">elseif</span> event <span class="sy0">==</span> <span class="st0">"changed"</span> <span class="kw1">then</span>
<span class="co1">-- 输入框内容发生变化</span>
<span class="kw1">elseif</span> event <span class="sy0">==</span> <span class="st0">"ended"</span> <span class="kw1">then</span>
<span class="co1">-- 输入结束</span>
<span class="kw1">elseif</span> event <span class="sy0">==</span> <span class="st0">"return"</span> <span class="kw1">then</span>
<span class="co1">-- 从输入框返回</span>
<span class="kw1">end</span>
<span class="kw1">end</span>
<span class="kw1">local</span> editbox <span class="sy0">=</span> ui<span class="sy0">.</span>newEditBox<span class="br0">(</span><span class="br0">{</span>
image <span class="sy0">=</span> <span class="st0">"EditBox.png"</span><span class="sy0">,</span>
listener <span class="sy0">=</span> onEdit<span class="sy0">,</span>
size <span class="sy0">=</span> CCSize<span class="br0">(</span><span class="nu0">200</span><span class="sy0">,</span> <span class="nu0">40</span><span class="br0">)</span>
<span class="br0">}</span><span class="br0">)</span></pre>
<p>
note:构造输入框时,请使用setPlaceHolder来设定初始文本显示。setText为出现输入法后的默认文本。
</p>
<p>
note:事件触发机制,player模拟器上与真机不同,请使用真机实测(不同ios版本貌似也略有不同)。
</p>
<p>
note:changed事件中,需要条件性使用setText(如trim或转化大小写等),否则在某些ios版本中会造成死循环。
</p>
<pre class="code lua"><span class="co1">--错误,会造成死循环</span>
editbox<span class="sy0">:</span>setText<span class="br0">(</span><span class="kw3">string</span><span class="sy0">.</span>trim<span class="br0">(</span>editbox<span class="sy0">:</span>getText<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span></pre>
<pre class="code lua"><span class="co1">--正确,不会造成死循环</span>
<span class="kw1">local</span> _text <span class="sy0">=</span> editbox<span class="sy0">:</span>getText<span class="br0">(</span><span class="br0">)</span>
<span class="kw1">local</span> _trimed <span class="sy0">=</span> <span class="kw3">string</span><span class="sy0">.</span>trim<span class="br0">(</span>_text<span class="br0">)</span>
<span class="kw1">if</span> _trimed <span class="sy0">~=</span> _text <span class="kw1">then</span>
editbox<span class="sy0">:</span>setText<span class="br0">(</span>_trimed<span class="br0">)</span>
<span class="kw1">end</span></pre>
<p>
~~
</p>
</div>
<h2 class="sectionedit3" id="uinewimagemenuitem">ui.newImageMenuItem</h2>
<div class="level2">
<p>
创建一个图像菜单项,并返回 CCMenuItemSprite 对象。
</p>
<p>
格式:
</p>
<pre class="code lua">item <span class="sy0">=</span> ui<span class="sy0">.</span>newImageMenuItem<span class="br0">(</span>参数表格对象<span class="br0">)</span></pre>
<p>
可用参数:
</p>
<ul>
<li class="level1"><div class="li">image: 正常状态的按钮图像</div>
</li>
<li class="level1"><div class="li">imageSelected: 按钮按下时的图像(可选)</div>
</li>
<li class="level1"><div class="li">imageDisabled: 按钮被禁用时的图像(可选)</div>
</li>
<li class="level1"><div class="li">listener: 回调函数</div>
</li>
<li class="level1"><div class="li">tag: 按钮的 Tag,会传入回调函数。多个按钮使用同一个回调函数时,可根据 Tag 区分哪一个按钮被按下(可选)</div>
</li>
<li class="level1"><div class="li">x, y: 坐标(可选)</div>
</li>
<li class="level1"><div class="li">sound: 按钮按下时播放什么音效(可选)</div>
</li>
</ul>
<p>
用法示例:
</p>
<pre class="code lua"><span class="kw1">local</span> <span class="kw1">function</span> onButtonClicked<span class="br0">(</span>tag<span class="br0">)</span>
<span class="co1">-- 按钮被按下</span>
<span class="kw1">end</span>
<span class="kw1">local</span> item <span class="sy0">=</span> ui<span class="sy0">.</span>newImageMenuItem<span class="br0">(</span><span class="br0">{</span>
image <span class="sy0">=</span> <span class="st0">"Button.png"</span><span class="sy0">,</span>
imageSelected <span class="sy0">=</span> <span class="st0">"ButtonSelected.png"</span><span class="sy0">,</span>
listener <span class="sy0">=</span> onButtonClicked
<span class="br0">}</span><span class="br0">)</span>
<span class="co1">-- 创建菜单并加入场景,否则菜单项不会工作</span>
<span class="kw1">local</span> menu <span class="sy0">=</span> ui<span class="sy0">.</span>newMenu<span class="br0">(</span><span class="br0">{</span>item<span class="br0">}</span><span class="br0">)</span>
scene<span class="sy0">:</span>addChild<span class="br0">(</span>menu<span class="br0">)</span></pre>
<p>
~~
</p>
</div>
<h2 class="sectionedit4" id="uinewttflabelmenuitem">ui.newTTFLabelMenuItem</h2>
<div class="level2">
<p>
创建一个文字标签菜单项,并返回 CCMenuItemLabel 对象。
</p>
<p>
格式:
</p>
<pre class="code lua">item <span class="sy0">=</span> ui<span class="sy0">.</span>newTTFLabelMenuItem<span class="br0">(</span>参数表格对象<span class="br0">)</span></pre>
<p>
可用参数:
</p>
<ul>
<li class="level1"><div class="li">listener: 回调函数</div>
</li>
<li class="level1"><div class="li">tag: 按钮的 Tag,会传入回调函数。多个按钮使用同一个回调函数时,可根据 Tag 区分哪一个按钮被按下(可选)</div>
</li>
<li class="level1"><div class="li">x, y: 坐标(可选)</div>
</li>
<li class="level1"><div class="li">sound: 按钮按下时播放什么音效(可选)</div>
</li>
</ul>
<p>
以及所有可以用于 ui.newTTFLabel() 的参数。
</p>
<p>
~~
</p>
</div>
<h2 class="sectionedit5" id="uinewmenu">ui.newMenu</h2>
<div class="level2">
<p>
创建菜单,并返回 CCMenu 对象。
</p>
<p>
格式:
</p>
<pre class="code lua">menu <span class="sy0">=</span> ui<span class="sy0">.</span>newMenu<span class="br0">(</span><span class="br0">{</span>菜单项对象<span class="sy0">,</span> <span class="br0">[</span>菜单项对象 <span class="sy0">...</span><span class="br0">]</span><span class="br0">}</span><span class="br0">)</span></pre>
<p>
~~
</p>
</div>
<h2 class="sectionedit6" id="uinewbmfontlabel">ui.newBMFontLabel</h2>
<div class="level2">
<p>
用位图字体创建文本显示对象,并返回 CCLabelBMFont 对象。
</p>
<p>
格式:
</p>
<pre class="code lua">label <span class="sy0">=</span> ui<span class="sy0">.</span>newBMFontLabel<span class="br0">(</span>参数表格对象<span class="br0">)</span></pre>
<p>
可用参数:
</p>
<ul>
<li class="level1"><div class="li">text: 要显示的文本</div>
</li>
<li class="level1"><div class="li">font: 字体文件名</div>
</li>
<li class="level1"><div class="li">align: 文字的水平对齐方式(可选)</div>
</li>
<li class="level1"><div class="li">x, y: 坐标(可选)</div>
</li>
</ul>
<p>
用法示例:
</p>
<pre class="code lua"><span class="kw1">local</span> label <span class="sy0">=</span> ui<span class="sy0">.</span>newBMFontLabel<span class="br0">(</span><span class="br0">{</span>
text <span class="sy0">=</span> <span class="st0">"Hello"</span><span class="sy0">,</span>
font <span class="sy0">=</span> <span class="st0">"UIFont.fnt"</span><span class="sy0">,</span>
<span class="br0">}</span><span class="br0">)</span></pre>
<p>
BMFont 的原理是用包含多个字母的位图字体来创建文字内容,因此需要用专门的工具来创建 BMFont 字体:
</p>
<ul>
<li class="level1"><div class="li"><a href="http://www.angelcode.com/products/bmfont/" class="urlextern" target="_blank" title="http://www.angelcode.com/products/bmfont/" rel="nofollow">Bitmap Font Generator</a> 免费工具,Windows 版</div>
</li>
<li class="level1"><div class="li"><a href="http://www.71squared.com/en/glyphdesigner" class="urlextern" target="_blank" title="http://www.71squared.com/en/glyphdesigner" rel="nofollow">Glyph Designer</a> 收费工具,Mac 版</div>
</li>
</ul>
<p>
BMFont 通常用于显示英文内容,因为英文字母加数字和常用符号也不多,生成的 BMFont 文件较小。如果是中文,应该用 TTFLabel。
</p>
<p>
~~
</p>
</div>
<h2 class="sectionedit7" id="uinewttflabel">ui.newTTFLabel</h2>
<div class="level2">
<p>
使用 TTF 字体创建文字显示对象,并返回 CCLabelTTF 对象。
</p>
<p>
格式:
</p>
<pre class="code lua">label <span class="sy0">=</span> ui<span class="sy0">.</span>newTTFLabel<span class="br0">(</span>参数表格对象<span class="br0">)</span></pre>
<p>
可用参数:
</p>
<ul>
<li class="level1"><div class="li">text: 要显示的文本</div>
</li>
<li class="level1"><div class="li">font: 字体名,如果是非系统自带的 TTF 字体,那么指定为字体文件名</div>
</li>
<li class="level1"><div class="li">size: 文字尺寸,因为是 TTF 字体,所以可以任意指定尺寸</div>
</li>
<li class="level1"><div class="li">color: 文字颜色(可选),用 ccc3() 指定,默认为白色</div>
</li>
<li class="level1"><div class="li">align: 文字的水平对齐方式(可选)</div>
</li>
<li class="level1"><div class="li">valign: 文字的垂直对齐方式(可选),仅在指定了 dimensions 参数时有效</div>
</li>
<li class="level1"><div class="li">dimensions: 文字显示对象的尺寸(可选),使用 CCSize() 指定</div>
</li>
<li class="level1"><div class="li">x, y: 坐标(可选)</div>
</li>
</ul>
<p>
align 和 valign 参数可用的值:
</p>
<ul>
<li class="level1"><div class="li">ui.TEXT_ALIGN_LEFT 左对齐</div>
</li>
<li class="level1"><div class="li">ui.TEXT_ALIGN_CENTER 水平居中对齐</div>
</li>
<li class="level1"><div class="li">ui.TEXT_ALIGN_RIGHT 右对齐</div>
</li>
<li class="level1"><div class="li">ui.TEXT_VALIGN_TOP 垂直顶部对齐</div>
</li>
<li class="level1"><div class="li">ui.TEXT_VALIGN_CENTER 垂直居中对齐</div>
</li>
<li class="level1"><div class="li">ui.TEXT_VALIGN_BOTTOM 垂直底部对齐</div>
</li>
</ul>
<p>
用法示例:
</p>
<pre class="code lua"><span class="co1">-- 创建一个居中对齐的文字显示对象</span>
<span class="kw1">local</span> label <span class="sy0">=</span> ui<span class="sy0">.</span>newTTFLabel<span class="br0">(</span><span class="br0">{</span>
text <span class="sy0">=</span> <span class="st0">"Hello, World"</span><span class="sy0">,</span>
font <span class="sy0">=</span> <span class="st0">"Marker Felt"</span><span class="sy0">,</span>
size <span class="sy0">=</span> <span class="nu0">64</span><span class="sy0">,</span>
align <span class="sy0">=</span> ui<span class="sy0">.</span>TEXT_ALIGN_CENTER <span class="co1">-- 文字内部居中对齐</span>
<span class="br0">}</span><span class="br0">)</span>
<span class="co1">-- 左对齐,并且多行文字顶部对齐</span>
<span class="kw1">local</span> label <span class="sy0">=</span> ui<span class="sy0">.</span>newTTFLabel<span class="br0">(</span><span class="br0">{</span>
text <span class="sy0">=</span> <span class="st0">"Hello, World<span class="es1">\n</span>您好,世界"</span><span class="sy0">,</span>
font <span class="sy0">=</span> <span class="st0">"Arial"</span><span class="sy0">,</span>
size <span class="sy0">=</span> <span class="nu0">64</span><span class="sy0">,</span>
color <span class="sy0">=</span> ccc3<span class="br0">(</span><span class="nu0">255</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">)</span><span class="sy0">,</span> <span class="co1">-- 使用纯红色</span>
align <span class="sy0">=</span> ui<span class="sy0">.</span>TEXT_ALIGN_LEFT<span class="sy0">,</span>
valign <span class="sy0">=</span> ui<span class="sy0">.</span>TEXT_VALIGN_TOP<span class="sy0">,</span>
dimensions <span class="sy0">=</span> CCSize<span class="br0">(</span><span class="nu0">400</span><span class="sy0">,</span> <span class="nu0">200</span><span class="br0">)</span>
<span class="br0">}</span><span class="br0">)</span></pre>
<p>
~~
</p>
</div>
<h2 class="sectionedit8" id="uinewttflabelwithshadow">ui.newTTFLabelWithShadow</h2>
<div class="level2">
<p>
创建带阴影的 TTF 文字显示对象,并返回 CCLabelTTF 对象。
</p>
<p>
相比 ui.newTTFLabel() 增加一个参数:
</p>
<ul>
<li class="level1"><div class="li">shadowColor: 阴影颜色(可选),用 ccc3() 指定,默认为黑色</div>
</li>
</ul>
<p>
~~
</p>
</div>
<h2 class="sectionedit9" id="uinewttflabelwithoutline">ui.newTTFLabelWithOutline</h2>
<div class="level2">
<p>
创建带描边效果的 TTF 文字显示对象,并返回 CCLabelTTF 对象。
</p>
<p>
相比 ui.newTTFLabel() 增加一个参数:
</p>
<ul>
<li class="level1"><div class="li">outlineColor: 描边颜色(可选),用 ccc3() 指定,默认为黑色
</div>
</li>
</ul>
</div>
<!-- wikipage stop -->
</div>
<div class="docInfo">zh_cn/api/framework.ui.txt · Last modified: 2013/09/06 13:16 by Aaron Teng</div>
</div></div>
<!-- /content -->
<hr class="a11y">
<!-- PAGE ACTIONS -->
<div id="dokuwiki__pagetools">
<h3 class="a11y">Page Tools</h3>
<div class="tools">
<ul>
<li>
<a href="index.html" class="action backlink" rel="nofollow" title="home">
<span>home</span>
</a>
</li>
<li>
<a href="#dokuwiki__top" class="action top" accesskey="t" rel="nofollow" title="Back to top [T]">
<span>Back to top</span>
</a>
</li>
</ul>
</div>
</div>
</div>
<!-- /wrapper -->
</div>
</div>
<!-- /site -->
<div class="no">
<img src="zh_cn_api%20%5Bquick-cocos2d-x%20docs%5D_files/indexer.gif" alt="" height="1" width="2">
</div>
<div id="screen__mode" class="no"></div>
<!--[if ( lte IE 7 | IE 8 ) ]></div><![endif]-->
</body>
</html>