最近着手写UI,发现很多坑。比如文本框的对齐问题,无论如何设置textAlign,都没法实现上下左右对齐,被策划美术鄙视了一番,遂应用简单数学知识实现之。发来给有需求的朋友,同时也欢迎指正。
左对齐:
/**
- @param textField 需要对齐的文本框
- @param position 左对齐时的坐标点
- @param showWidth 文本框设置的总宽度
- example tools.TextAlignTools.HLeftAlign(iconNumTxt,cc.p(170,60),95);
*/
tools.TextAlignTools.HLeftAlign = function(textField,position,showWidth){
if(textField == null || textField == undefined) return;
textField.anchorX = 0.5;
var posX = position.x - (showWidth - textField.getContentSize().width)/2;
textField.setPosition(posX,position.y);
}
右对齐:
tools.TextAlignTools.HRightAligin = function(textField,position,showWidth){
if(textField == null || textField == undefined) return;
textField.anchorX= 0.5;
var posX = position.x + (showWidth - textField.getContentSize().width)/2;
textField.setPosition(posX,position.y);
}
上对齐:
tools.TextAlignTools.VTopAlign = function(textField,position,showHeight){
if(textField == null || textField == undefined) return;
textField.anchorY= 0.5;
var posY = position.x + (showHeight - textField.getContentSize().height)/2;
textField.setPosition(position.x,posY);
}
下对齐:
game.TextAlignTools.VTopAlign = function(textField,position,showHeight){
if(textField == null || textField == undefined) return;
textField.anchorY= 0.5;
var posY = position.x - (showHeight - textField.getContentSize().height)/2;
textField.setPosition(position.x,posY);
}
TextAlignTools.txt (3 KB)
只测试了左右对齐,成功。上下对齐有需求的可是试试。 