[]HTMLDivElement 怎么设置图片和文本底对齐 或 居中对齐

已邀请:

唐皆

赞同来自:

可以修改Laya的Layout类:
    函数_multiLineLayout
```
    //生成排版的行
        for (i = 0; i < n; i++) {
            oneLayout = elements[i];
            if (oneLayout == null) {
                if (!tLineFirstKey) {
                    x += Layout.DIV_ELEMENT_PADDING;
                }
                curLine.wordStartIndex = curLine.elements.length;
                continue;
            }
            tLineFirstKey = false;
            if (oneLayout instanceof IHtml.HTMLBrElement) {
                addLine();
                curLine.y = y;
                curLine.h = lineHeight;
                continue;
            } else if (oneLayout._isChar()) {
                htmlWord = <HTMLChar>oneLayout;
                w = htmlWord.width + htmlWord.style.letterSpacing;  // 下面要用到w,所以先计算(Bug:英文的宽度按照前面的汉字的宽度计算导致错误换行)
                h = htmlWord.height;
                if (!htmlWord.isWord) //如果是完整单词
                {
                    if (lines.length > 0 && (x + w) > width && curLine.wordStartIndex > 0) //如果完整单词超界,需要单词开始折到下一行
                    {
                        var tLineWord: number = 0;
                        tLineWord = curLine.elements.length - curLine.wordStartIndex + 1;
                        curLine.elements.length = curLine.wordStartIndex;
                        i -= tLineWord;
                        addLine();
                        continue;
                    }
                    newLine = false;
                    tWordWidth += htmlWord.width;
                } else {
                    newLine = nextNewline || (htmlWord.char === '\n');
                    curLine.wordStartIndex = curLine.elements.length;
                } 
                nextNewline = false; 
                newLine = newLine || ((x + w) > width);
                newLine && addLine();
                curLine.minTextHeight = Math.min(curLine.minTextHeight, oneLayout.height);
            } else {
                curStyle = oneLayout._getCSSStyle();
                sprite = (<HTMLElement>oneLayout);
                curPadding = curStyle.padding;
                //curStyle._getCssFloat() === 0 || (endAdjust = true);
                newLine = nextNewline || curStyle.getLineElement();
                w = sprite.width + curPadding[1] + curPadding[3] + curStyle.letterSpacing;
                h = sprite.height + curPadding[0] + curPadding[2];
                nextNewline = curStyle.getLineElement();
                newLine = newLine || ((x + w) > width && curStyle.wordWrap);
                newLine && addLine();
            } 
            curLine.elements.push(oneLayout);
            curLine.h = Math.max(curLine.h, h);//计算最大宽和高
            oneLayout.x = x;
            /////------------------------------------------------------
///此处的xxx是个自定义变量,用于偏移Y坐标使用,例如:可以设置图片往上偏移a,这样图片就可以居中显示了
            ///`<img width='40' height='40' sy='a' src='url'></img>`
oneLayout.y = y + Number(oneLayout["sy"] || 0);
////--------------------------------------------------
            x += w;
            curLine.w = x - letterSpacing;
            curLine.y = y;
            maxWidth = Math.max(x + exWidth, maxWidth);
        }
```

要回复问题请先

商务合作
商务合作