解决Createjs 文本框在不同平台上,垂直方向上偏移错位的问题

主要解决以下情况:

例如下图:

核心代码如下:(实现原理,还原代码格式自行查看)

var offsetY;//获得此手机上的文本框Y的偏移量

canvas = document.getElementById("canv");

stage = new createjs.Stage(canvas);

getOffsetY()


function getOffsetY(){var _s=this;var topSpace=0,bottomSpace=0;var mc=new createjs.MovieClip();var tx=new createjs.Text();tx.color="#FFF";tx.textAlign="left";tx.font="bold 100px 'Arial'";tx.textBaseline="top";tx.lineHeight=100;tx.text="";mc.addChild(tx);var p=new createjs.Graphics();p.beginFill("#F00");p.dr(0,0,1,1);p.endFill();var sp=new createjs.Shape(p);mc.addChild(sp);function checkTop(){sp.x=tx.x+15;sp.y=tx.y;for(;;){sp.y+=1;topSpace++;var hit=tx.hitTest(sp.x,sp.y);if(hit){checkBottom();break}}}function checkBottom(){sp.y=tx.y+100;for(;;){sp.y-=1;bottomSpace++;var hit=tx.hitTest(sp.x,sp.y);if(hit){offsetY=(bottomSpace-topSpace)*0.5;break}}}checkTop()};

使用方式:

//animte IDE内对齐后,再使用, 

text.y+=0.26*offsetY

0.26  是当前文本款的字号, 26号对应0.26  其他同理。

特殊情况,如果用户系统更改了自己系统的默认字体,可能会引起错误。

有解决办法,待近期有时间再完善

原文地址:https://www.cnblogs.com/luoeeyang/p/14298161.html