Ui.newTTFLabel 对齐显示错误(找到问题了)

framework里
ui.lua
https://github.com/chukong/quick-cocos2d-x/blob/master/framework/ui.lua

if label then
    label:setColor(color)

    function label:realign(x, y)
        if textAlign == ui.TEXT_ALIGN_LEFT then
            label:setPosition(math.round(x +label:getContentSize().width / 2), y)
        elseif textAlign == ui.TEXT_ALIGN_RIGHT then
            label:setPosition(x - math.round(label:getContentSize().width / 2), y)
        else
            label:setPosition(x, y)
        end
    end

    if x and y then label:realign(x, y) end
end

if textAlign == ui.TEXT_ALIGN_LEFT then
此时 加上了宽度的一半
我之前是 as3 开发者
left align时 左对齐 应该是 靠左 感觉不应该 加宽度的一半

猜测之前 是默认设置

local textAlign
= params.align
or ui.TEXT_ALIGN_LEFT
这句和 默认的 anchorPoint设置有些出入
调整正常的效果 就需要 统一 默认 anchorPoint center_middle 和 TEXT_ALIGN_LEFT
if TEXT_ALIGN_LEFT 就写了 中中的逻辑

--------------之前的bug-------------------
local name = ui.newTTFLabel({
text = “名字”,
size = 18,
color = ccc3(121, 50, 27),
x = 100,
y = 200
})
name:align(display.LEFT_TOP)
name:addTo(self)

设置了 align(display.LEFT_TOP) 后
输入文字越长 位置向右偏移 越多
具体表现和
display.CENTER_TOP 一样

设置 center_top 变右对齐了
rignt_top 又显示为左对齐了

ccsprit align 工作正常

试了下,没这个情况啊?

你事怎么调用的?

ui.newTTFLabel 设置对其应该是ui.TEXT_ALIGN_XX 系列, align(display.LEFT_TOP) 是设置锚点的。

local name = ui.newTTFLabel({
text = “名字”,
size = 18,
color = ccc3(121, 50, 27),
x = 100,
y = 200
})
name:align(display.LEFT_TOP)
name:addTo(self)

伪代码

sprite 使用相同的 align 不存在对齐偏差的问题

没有设置 dimensions 所以 应该用不到 ui.TEXT_ALIGN_XX

文字对齐应该在newTTFLabel时传入align属性。
name:align这样的调用是对整个Label的对齐调整。

ui.newTTFLabel 如果不传对齐 默认是左对其,会对其位置做相应偏移设置,

function ui.newTTFLabel(params)    assert(type(params) == "table",           " newTTFLabel() invalid params")
    local text       = tostring(params.text)    local font       = params.font or ui.DEFAULT_TTF_FONT    local size       = params.size or ui.DEFAULT_TTF_FONT_SIZE    local color      = params.color or display.COLOR_WHITE    local textAlign  = params.align or ui.TEXT_ALIGN_LEFT    local textValign = params.valign or ui.TEXT_VALIGN_CENTER    local x, y       = params.x, params.y    local dimensions = params.dimensions
    assert(type(size) == "number",           " newTTFLabel() invalid params.size")
    local label    if dimensions then        label = CCLabelTTF:create(text, font, size, dimensions, textAlign, textValign)    else        label = CCLabelTTF:create(text, font, size)    end
    if label then        label:setColor(color)
        function label:realign(x, y)            if textAlign == ui.TEXT_ALIGN_LEFT then                label:setPosition(math.round(x + label:getContentSize().width / 2), y)            elseif textAlign == ui.TEXT_ALIGN_RIGHT then                label:setPosition(x - math.round(label:getContentSize().width / 2), y)            else                label:setPosition(x, y)            end        end
        if x and y then label:realign(x, y) end    end
    return labelend

你这样使用应该是可以满足你的需求。

local name = ui.newTTFLabel({    text = "名字",    size = 64,    color = ccc3(121, 50, 27),    x = 100,    y = 200,    align = ui.TEXT_ALIGN_CENTER  })  name:align(display.LEFT_TOP)  name:addTo(self)

— Begin quote from ____

引用第6楼yangtao19cs于2014-07-02 14:26发表的 回 4楼(zongwan) 的帖子 :
ui.newTTFLabel 如果不传对齐 默认是左对其,会对其位置做相应偏移设置,

function ui.newTTFLabel(params)    assert(type(params) == "table",           " newTTFLabel() invalid params")
    local text       = tostring(params.text)    local font       = params.font or ui.DEFAULT_TTF_FONT    local size       = params.size or ui.DEFAULT_TTF_FONT_SIZE    local color      = params.color or display.COLOR_WHITE    local textAlign  = params.align or ui.TEXT_ALIGN_LEFT    local textValign = params.valign or ui.TEXT_VALIGN_CENTER    local x, y       = params.x, params.y    local dimensions = params.dimensions
    assert(type(size) == "number",           " newTTFLabel() invalid params.size")
....... http://www.cocoachina.com/bbs/job.php?action=topost&tid=212328&pid=994047
![](p_w_picpath/back.gif)

谢谢 这样 可以临时解决问题

— End quote

你也可以不用ui 来创建 自己写一个创建方法,满足你的需求的。