LayaAir-文本
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5 <title>layer文本样式</title>
6 <style>
7 * { margin: 0; padding: 0; }
8 </style>
9 </head>
10
11 <body></body>
12
13 <!-- js需要在</body>之后引用,不能在head里引用,否则会有报错。 -->
14 <script src="libs/laya.core.js"></script>
15 <script>
16 // 1.舞台初始化
17 Laya.init(550, 400); // 设置舞台宽高
18 Laya.stage.bgColor = '#666'; // 设置舞台背景颜色
19 Laya.stage.scaleMode = laya.display.Stage.SCALE_SHOWALL;// 设置舞台的缩放模式
20
21 // 2.实例化文本对象
22 var txt = new laya.display.Text(); // 新建一个文本对象
23 txt.text = "Layabox是性能最强的HTML5引擎"; // 文本内容设置
24 txt.asPassword = false; // 是否密码文本,默认为false,设为true时文本以*号显示
25
26 // 3.文本位置与盒子模型
27 txt.pos(75, 50); // 显示位置
28 txt.width = 400; // 文本对象宽度
29 txt.height = 320; // 文本对象高度
30 txt.borderColor = '#f00'; // 边框颜色
31 txt.wordWrap = true; // 是否换行
32 txt.align = 'center'; // 水平对齐方式,默认为left(左对齐),也可设置center(水平居中)或right(右对齐)
33 txt.valign = 'middle'; // 垂直对齐方式,默认为top(上对齐),也可设置middle(垂直居中)或bottom(底部对齐)
34
35 // 4.文本样式
36 txt.font = 'Mocrosoft YaHei'; // 字体,默认为Arial
37 txt.fontSize = 40; // 字体大小
38 txt.leading = 5; // 垂直行间距,以像素为单位
39 txt.bold = true; // 是否加粗,默认为false
40 txt.italic = false; // 是否为斜体,默认为false
41 txt.color = '#fff'; // 文本颜色
42 txt.bgColor = '#358'; // 背景颜色,颜色值设置与css一致, 可设置rgb() rgba()
43 txt.stroke = 7.5; // 文字描边宽度,以像素为单位
44 txt.strokeColor = 'rgba(255, 255, 0, 0.5)'; // 文字描边颜色
45
46 // 5.文本属性获取
47 console.log('横向滚动量为: ' + txt.maxScrollX);
48 console.log('纵向滚动量为: ' + txt.maxScrollY);
49 console.log('横向可滚动最大值为: ' + txt.maxScrollX);
50 console.log('纵向可滚动最大值为: ' + txt.maxScrollY);
51 console.log('文本宽度为: ' + txt.textWidth);
52 console.log('文本高度为: ' + txt.textHeight);
53 console.log('边距信息[上,右,下,左]: ' + txt.padding);
54 console.log(txt.getCharPoint(0)); // 返回指定字符的位置信息
55 console.log(txt.getGraphicBounds()); // 返回实例中的绘图对象的显示区域
56
57 // 6.方法
58 txt.changeText('你好'); // 快速更改显示文本,不进行排版计算
59 // txt.typeset(); // 对文本对象重新进行宽高计算、渲染和重绘
60 // txt.destroy(); // 销毁此对象
61 // txt.lang(); // 设置语言包
62 // txt.registerBitmapFont(); // 注册位图字体
63 // txt.unregisterBitmapFont(); // 取消注册的位图字体
64
65 // 7.最后一步需要将定义好字体样式后需要将这个文本对象添加到舞台上
66 Laya.stage.addChild(txt);
67 </script>
68 </html>