let nowX = startX - (this._clipPoint ? this._clipPoint.x : 0);
for (var line of lines)
{
var word = line['text'];
x = nowX;
y = startY + lineHeight * i - (this._clipPoint ? this._clipPoint.y : 0);
if (tCurrBitmapFont)
{
var tWidth = this.width;
if (tCurrBitmapFont.autoScaleSize)
{
tWidth = this.width * bitmapScale;
}
tCurrBitmapFont.drawText(word, this, x, y, this.align, tWidth);
} else
{
if (Laya.Render.isWebGL)
{
this._words || (this._words = );
_word = this._words.length > (i - begin) ? this._words[i - begin] : new Laya.WordText();
_word.setText(word);
} else
{
_word = word;
}
//设置颜色、粗体等等
if (line["type"])
{
//如果有关键字设置的类型,则使用关键字
if (line["type"]["bold"] != undefined)
{
if (bold != line["type"]["bold"])
{
bold = line["type"]["bold"];
ctxFont = (this.italic ? "italic " : "") + (bold ? "bold " : "") + this.fontSize + "px " + (Browser.onIPhone ? (laya.display.Text._fontFamilyMap[this.font] || this.font) : this.font);
Browser.context.font = ctxFont;
}
}
if (line["type"]["underline"] != undefined)
{
if (underline != line["type"]["underline"])
{
underline = line["type"]["underline"]
}
}
if (line["type"]["color"])
{
if (color != line["type"]["color"])
{
color = line["type"]["color"]
//this._getCSSStyle().color = color;
}
}
}
if (underline)
{
//绘制下划线
this.underColor = color;
this.underLineWidth = this.getTextWidth(line['text']);
this.drawUnderline(textAlgin, x, y, i);
}
if (style.stroke)
graphics.fillBorderText(_word, x, y, ctxFont, color, style.strokeColor, style.stroke, textAlgin);
else
graphics.fillText(_word, x, y, ctxFont, color, textAlgin);
}
//不再是每行绘制一次,而是每行根据关键字绘制多次
nowX += this.getTextWidth(line['text']);
}
}
if (tCurrBitmapFont && tCurrBitmapFont.autoScaleSize)
{
var tScale = 1 / bitmapScale;
this.scale(tScale, tScale);
}
if (this._clipPoint)
graphics.restore();
this._startX = startX;
this._startY = startY;
}
parseLines(text)
{
var needWordWrapOrTruncate = this.wordWrap || this.overflow == Text.HIDDEN;
if (needWordWrapOrTruncate)
{
var wordWrapWidth = this.getWordWrapWidth();
}
if (this._currBitmapFont)
{
this._charSize.width = this._currBitmapFont.getMaxWidth();
this._charSize.height = this._currBitmapFont.getMaxHeight();
} else
{
var measureResult = Browser.context.measureText(Text._testWord);
this._charSize.width = measureResult.width;
this._charSize.height = (measureResult.height || this.fontSize);
};
//TS的正则匹配需要加转义
var lines = text.replace(/\\r\\n/g, "\n").split("\\n");
for (var i = 0, n = lines.length; i < n; i++)
{
var line = lines[i];
/**
* 绘制下划线
* @param align
* @param x
* @param y
* @param lineIndex
*/
drawUnderline(align, x, y, lineIndex)
{
var lineWidth = this.underLineWidth;
switch (align)
{
case 'center':
x -= lineWidth / 2;
break;
case 'right':
x -= lineWidth;
break;
case 'left':
default:
break;
}
y += this._charSize.height;
this._graphics.drawLine(x, y, x + lineWidth, y, this.underColor || this.color, 1);
}
/**
* 取出关键字即所在的索引
* @param _text
*/
private parseType(_text: string)
{
let text = _text.replace(/(\\r\\n)|(\\n)/g, "")
let typeKey = text.match(/{.+?}/);
if (!typeKey)
return _text;
let ketStr = typeKey[0];
while (ketStr)
{
let typeIndex = text.indexOf(ketStr)
if (!this.typeList[typeIndex])
{
this.typeIndexList.push(typeIndex);
this.typeList[typeIndex] = {}
}
switch (ketStr)
{
case '{b}':
this.typeList[typeIndex]["bold"] = true;
break;
case '{/b}':
this.typeList[typeIndex]["bold"] = false;
break;
case '{u}':
this.typeList[typeIndex]["underline"] = true;
break;
case '{/u}':
this.typeList[typeIndex]["underline"] = false;
break;
default:
this.typeList[typeIndex]["color"] = ketStr.slice(1, -1);
break;
}
_text = _text.replace(ketStr, '');
text = text.replace(ketStr, '');
typeKey = text.match(/{.+?}/);
if (!typeKey)
return _text;
ketStr = typeKey[0];
}
return _text;
}
/**
* 把按行划分的文本再依据关键字划分一次
*/
private parseTypeLines()
{
let lines = this._lines;
let index = 0;
for (let oldLine of lines)
{
let newLines = ;
let oldIndex = 0;
7 个回复
he853066732
赞同来自: 176*****233 、yjn
排版仍然调用Laya的排版函数(这里之所以重写是因为TS的正则需要加转义字符)
目前发现的BUG:右对齐且最后一行有数字“1”时排版不正常,发现是
getTextWidth( ) 函数的结果和在屏幕上实际显示的宽度不同导致。
yjn
赞同来自: 小强1
he853066732
赞同来自:
176*****233
赞同来自:
麻辣干脆面
赞同来自:
chunjine
赞同来自:
devx
赞同来自: