Commit ba3576ae authored by limingzhe's avatar limingzhe

fix: 文本自适应

parent bb4d34b6
......@@ -102,28 +102,28 @@
"__id__": 79
},
{
"__id__": 97
"__id__": 101
},
{
"__id__": 103
"__id__": 107
},
{
"__id__": 109
"__id__": 113
},
{
"__id__": 117
"__id__": 121
}
],
"_active": true,
"_components": [
{
"__id__": 137
"__id__": 141
},
{
"__id__": 138
"__id__": 142
},
{
"__id__": 139
"__id__": 143
}
],
"_prefab": null,
......@@ -3578,7 +3578,7 @@
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
-825,
-1638.806,
280,
0,
0,
......@@ -4254,12 +4254,18 @@
"_children": [
{
"__id__": 94
},
{
"__id__": 96
},
{
"__id__": 98
}
],
"_active": true,
"_components": [
{
"__id__": 96
"__id__": 100
}
],
"_prefab": null,
......@@ -4318,7 +4324,7 @@
"__id__": 93
},
"_children": [],
"_active": true,
"_active": false,
"_components": [
{
"__id__": 95
......@@ -4348,7 +4354,7 @@
"ctor": "Float64Array",
"array": [
9.706,
3.397,
4,
0,
0,
0,
......@@ -4389,8 +4395,8 @@
"_dstBlendFactor": 771,
"_string": "Label",
"_N$string": "Label",
"_fontSize": 70,
"_lineHeight": 70,
"_fontSize": 50,
"_lineHeight": 50,
"_enableWrapText": false,
"_N$file": null,
"_isSystemFontUsed": true,
......@@ -4405,6 +4411,196 @@
"_N$cacheMode": 0,
"_id": "19riTcfoFHC6rIuKU2L2Wo"
},
{
"__type__": "cc.Node",
"_name": "text_1",
"_objFlags": 0,
"_parent": {
"__id__": 93
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 97
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 122.34,
"height": 63
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
9.706,
27.774,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "cdB3Kh4+FG77GXqEawCT5I"
},
{
"__type__": "cc.Label",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 96
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_string": "Label",
"_N$string": "Label",
"_fontSize": 50,
"_lineHeight": 50,
"_enableWrapText": false,
"_N$file": null,
"_isSystemFontUsed": true,
"_spacingX": 0,
"_batchAsBitmap": false,
"_styleFlags": 0,
"_underlineHeight": 0,
"_N$horizontalAlign": 1,
"_N$verticalAlign": 1,
"_N$fontFamily": "Arial",
"_N$overflow": 0,
"_N$cacheMode": 0,
"_id": "b2tK0TUbxFF6LSEmxePNZz"
},
{
"__type__": "cc.Node",
"_name": "text_2",
"_objFlags": 0,
"_parent": {
"__id__": 93
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 99
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 122.34,
"height": 63
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
9.706,
-24.303,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "7dJPwVXfFE9Z4voDIqw1GC"
},
{
"__type__": "cc.Label",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 98
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_string": "Label",
"_N$string": "Label",
"_fontSize": 50,
"_lineHeight": 50,
"_enableWrapText": false,
"_N$file": null,
"_isSystemFontUsed": true,
"_spacingX": 0,
"_batchAsBitmap": false,
"_styleFlags": 0,
"_underlineHeight": 0,
"_N$horizontalAlign": 1,
"_N$verticalAlign": 1,
"_N$fontFamily": "Arial",
"_N$overflow": 0,
"_N$cacheMode": 0,
"_id": "11WdqbzeVIwKKPZuOgT+SX"
},
{
"__type__": "cc.Button",
"_name": "",
......@@ -4482,16 +4678,16 @@
},
"_children": [
{
"__id__": 98
"__id__": 102
},
{
"__id__": 100
"__id__": 104
}
],
"_active": false,
"_components": [
{
"__id__": 102
"__id__": 106
}
],
"_prefab": null,
......@@ -4547,13 +4743,13 @@
"_name": "img_1",
"_objFlags": 0,
"_parent": {
"__id__": 97
"__id__": 101
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 99
"__id__": 103
}
],
"_prefab": null,
......@@ -4609,7 +4805,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 98
"__id__": 102
},
"_enabled": true,
"_materials": [
......@@ -4641,13 +4837,13 @@
"_name": "img_2",
"_objFlags": 0,
"_parent": {
"__id__": 97
"__id__": 101
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 101
"__id__": 105
}
],
"_prefab": null,
......@@ -4703,7 +4899,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 100
"__id__": 104
},
"_enabled": true,
"_materials": [
......@@ -4735,7 +4931,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 97
"__id__": 101
},
"_enabled": true,
"_id": "22vw/sJf1FcqZqummVQC8S"
......@@ -4749,16 +4945,16 @@
},
"_children": [
{
"__id__": 104
"__id__": 108
},
{
"__id__": 106
"__id__": 110
}
],
"_active": false,
"_components": [
{
"__id__": 108
"__id__": 112
}
],
"_prefab": null,
......@@ -4814,13 +5010,13 @@
"_name": "img_1",
"_objFlags": 0,
"_parent": {
"__id__": 103
"__id__": 107
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 105
"__id__": 109
}
],
"_prefab": null,
......@@ -4876,7 +5072,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 104
"__id__": 108
},
"_enabled": true,
"_materials": [
......@@ -4908,13 +5104,13 @@
"_name": "img_2",
"_objFlags": 0,
"_parent": {
"__id__": 103
"__id__": 107
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 107
"__id__": 111
}
],
"_prefab": null,
......@@ -4970,7 +5166,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 106
"__id__": 110
},
"_enabled": true,
"_materials": [
......@@ -5002,7 +5198,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 103
"__id__": 107
},
"_enabled": true,
"_id": "99+apunKpKbK8WdfjFGb2F"
......@@ -5016,16 +5212,16 @@
},
"_children": [
{
"__id__": 110
"__id__": 114
},
{
"__id__": 112
"__id__": 116
}
],
"_active": true,
"_components": [
{
"__id__": 116
"__id__": 120
}
],
"_prefab": null,
......@@ -5081,13 +5277,13 @@
"_name": "ani",
"_objFlags": 0,
"_parent": {
"__id__": 109
"__id__": 113
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 111
"__id__": 115
}
],
"_prefab": null,
......@@ -5143,7 +5339,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 110
"__id__": 114
},
"_enabled": true,
"_materials": [
......@@ -5183,17 +5379,17 @@
"_name": "btn_go",
"_objFlags": 0,
"_parent": {
"__id__": 109
"__id__": 113
},
"_children": [
{
"__id__": 113
"__id__": 117
}
],
"_active": false,
"_components": [
{
"__id__": 115
"__id__": 119
}
],
"_prefab": null,
......@@ -5249,13 +5445,13 @@
"_name": "text",
"_objFlags": 0,
"_parent": {
"__id__": 112
"__id__": 116
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 114
"__id__": 118
}
],
"_prefab": null,
......@@ -5311,7 +5507,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 113
"__id__": 117
},
"_enabled": true,
"_materials": [
......@@ -5344,7 +5540,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 112
"__id__": 116
},
"_enabled": true,
"_materials": [
......@@ -5376,11 +5572,11 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 109
"__id__": 113
},
"_enabled": true,
"aniRole": {
"__id__": 110
"__id__": 114
},
"moveX": 500,
"_id": "e4RrOXSNJGd68Mq3UTYY0p"
......@@ -5394,13 +5590,13 @@
},
"_children": [
{
"__id__": 118
"__id__": 122
},
{
"__id__": 121
"__id__": 125
},
{
"__id__": 122
"__id__": 126
}
],
"_active": true,
......@@ -5458,11 +5654,11 @@
"_name": "font",
"_objFlags": 0,
"_parent": {
"__id__": 117
"__id__": 121
},
"_children": [
{
"__id__": 119
"__id__": 123
}
],
"_active": true,
......@@ -5520,13 +5716,13 @@
"_name": "BRLNSDB",
"_objFlags": 0,
"_parent": {
"__id__": 118
"__id__": 122
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 120
"__id__": 124
}
],
"_prefab": null,
......@@ -5582,7 +5778,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 119
"__id__": 123
},
"_enabled": true,
"_materials": [
......@@ -5617,7 +5813,7 @@
"_name": "img",
"_objFlags": 0,
"_parent": {
"__id__": 117
"__id__": 121
},
"_children": [],
"_active": true,
......@@ -5675,15 +5871,9 @@
"_name": "audio",
"_objFlags": 0,
"_parent": {
"__id__": 117
"__id__": 121
},
"_children": [
{
"__id__": 123
},
{
"__id__": 125
},
{
"__id__": 127
},
......@@ -5698,6 +5888,12 @@
},
{
"__id__": 135
},
{
"__id__": 137
},
{
"__id__": 139
}
],
"_active": true,
......@@ -5755,13 +5951,13 @@
"_name": "btn",
"_objFlags": 0,
"_parent": {
"__id__": 122
"__id__": 126
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 124
"__id__": 128
}
],
"_prefab": null,
......@@ -5817,7 +6013,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 123
"__id__": 127
},
"_enabled": true,
"_clip": {
......@@ -5836,13 +6032,13 @@
"_name": "right",
"_objFlags": 0,
"_parent": {
"__id__": 122
"__id__": 126
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 126
"__id__": 130
}
],
"_prefab": null,
......@@ -5898,7 +6094,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 125
"__id__": 129
},
"_enabled": true,
"_clip": {
......@@ -5917,13 +6113,13 @@
"_name": "bettery",
"_objFlags": 0,
"_parent": {
"__id__": 122
"__id__": 126
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 128
"__id__": 132
}
],
"_prefab": null,
......@@ -5979,7 +6175,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 127
"__id__": 131
},
"_enabled": true,
"_clip": {
......@@ -5998,13 +6194,13 @@
"_name": "error",
"_objFlags": 0,
"_parent": {
"__id__": 122
"__id__": 126
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 130
"__id__": 134
}
],
"_prefab": null,
......@@ -6060,7 +6256,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 129
"__id__": 133
},
"_enabled": true,
"_clip": {
......@@ -6079,13 +6275,13 @@
"_name": "bgm",
"_objFlags": 0,
"_parent": {
"__id__": 122
"__id__": 126
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 132
"__id__": 136
}
],
"_prefab": null,
......@@ -6141,7 +6337,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 131
"__id__": 135
},
"_enabled": true,
"_clip": {
......@@ -6160,13 +6356,13 @@
"_name": "opendoor",
"_objFlags": 0,
"_parent": {
"__id__": 122
"__id__": 126
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 134
"__id__": 138
}
],
"_prefab": null,
......@@ -6222,7 +6418,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 133
"__id__": 137
},
"_enabled": true,
"_clip": {
......@@ -6241,13 +6437,13 @@
"_name": "light",
"_objFlags": 0,
"_parent": {
"__id__": 122
"__id__": 126
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 136
"__id__": 140
}
],
"_prefab": null,
......@@ -6303,7 +6499,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 135
"__id__": 139
},
"_enabled": true,
"_clip": {
......@@ -6391,10 +6587,10 @@
"__id__": 71
},
"img_left": {
"__id__": 97
"__id__": 101
},
"img_right": {
"__id__": 103
"__id__": 107
},
"ani_star": {
"__id__": 69
......
......@@ -167,12 +167,19 @@ export default class SceneComponent extends MyCocosSceneComponent {
lastY = null;
}
if (data.itemType == 'text') {
data.text += 'aaa bbb cc dd';
let item = pg.view.clone(this.item_battery_text)
item.active = true;
item.x = 0;
let item_text = pg.view.find(item, 'item_text');
pg.view.setString(pg.view.find(item_text, 'text'), data.text)
this.setItemText(item_text, data.text);
pg.view.setColor(pg.view.find(item_text, 'text'), "#FEFE24")
pg.view.setColor(pg.view.find(item_text, 'text_1'), "#FEFE24")
pg.view.setColor(pg.view.find(item_text, 'text_2'), "#FEFE24")
for (let n = 1; n <= 6; n++) {
pg.view.visible(pg.view.find(item, 'bg/img_battery' + n), false)
......@@ -337,6 +344,13 @@ export default class SceneComponent extends MyCocosSceneComponent {
node.y = targetData.pos.y;
let text = pg.view.find(node, 'text');
text.active = true;
let text1 = pg.view.find(node, 'text_1');
let text2 = pg.view.find(node, 'text_2');
text1.active = false;
text2.active = false;
pg.view.setColor(text, '#1D8B38')
text.getComponent(cc.Label).fontSize = targetData.fontSize;
text.getComponent(cc.Label).overflow = cc.Label.Overflow.NONE;
......@@ -453,6 +467,97 @@ export default class SceneComponent extends MyCocosSceneComponent {
});
})
}
async setItemText(itemText, text) {
const maxW = 180;
const disH = 26;
const baseY = 4;
const labelNode1 = cc.find("text_1", itemText);
const labelNode2 = cc.find("text_2", itemText);
const label1 = labelNode1.getComponent(cc.Label);
const label2 = labelNode2.getComponent(cc.Label);
const textArr = text.split(' ');
let isLabel1Max = false;
let isLabel2Max = false;
const checkLabelWidthMax = async (label, text) => {
label.string = text;
await asyncDelay(0.01);
const labelWidth = label.node.getBoundingBox().width;
console.log('labelWidth: ', labelWidth);
if (labelWidth > maxW) {
return true;
}
return false;
}
const checkPutTextByScale = async (scale) => {
label1.node.scale = scale;
label2.node.scale = scale;
isLabel1Max = false;
isLabel2Max = false;
let label1Text = '';
let label2Text = '';
for (let i = 0; i < textArr.length; i++) {
let checkText = textArr[i];
if (!isLabel1Max && label1Text != '') {
checkText = label1Text + ' ' + checkText;
}
let isCurMax1 = await checkLabelWidthMax(label1, checkText);
if (!isLabel1Max && !isCurMax1) {
label1Text = checkText;
} else {
isLabel1Max = true;
label1.string = label1Text;
checkText = textArr[i];
if (label2Text != '') {
checkText = label2Text + ' ' + checkText;
}
let isCurMax2 = await checkLabelWidthMax(label2, checkText);
if (!isLabel2Max && !isCurMax2) {
label2Text = checkText;
label2.string = label2Text;
} else {
return false;
}
}
}
return true;
}
const testScale = [1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1];
for (let i = 0; i < testScale.length; i++) {
const scale = testScale[i];
const isCanPut = await checkPutTextByScale(scale);
if (isCanPut) {
if (!isLabel1Max) {
label1.node.y = baseY;
label2.node.active = false;
} else {
label1.node.y = baseY + disH * 0.5 + disH * scale * 0.5;
label2.node.y = baseY - disH * 0.5 - disH * scale * 0.5;
label2.node.active = true;
}
break;
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment