[LayaAir 2.0]【解决】富文本html下划线换行bug
开发中大家都会遇到 聊天窗口需要支持下划线点击展示装备道具等需求。
修改2.0版本 laya.html.js库里的代码
修改2.0版本 laya.html.js库里的代码
__proto.renderSelfToGraphic=function(graphic,gX,gY,recList){
var cssStyle=this.style;
var words=this._getWords();
var i=0,len=0;
if (words){
len=words.length;
var a;
if (cssStyle){
var font=cssStyle.font;
var color=cssStyle.color;
if (cssStyle.stroke){
var stroke=cssStyle.stroke;
stroke=parseInt(stroke);
var strokeColor=cssStyle.strokeColor;
graphic.fillBorderWords(words,gX,gY,font,color,strokeColor,stroke);
}else {
graphic.fillWords(words,gX,gY,font,color);
}
if (this.href){
var lastIndex=words.length-1;
var lastWords=words[lastIndex];
var lineY=lastWords.y+lastWords.height;
if(cssStyle.textDecoration!="none"){
if(words[0].y != words[words.length-1].y){
let index = 0;
let slsIndex;//secondLineStartIndex
while(true){
if(words[index].y != words[index+1].y){
slsIndex = index;
break;
}
index++;
}
let firstlineY = words[0].y+words[0].height;
graphic.drawLine(words[0].x,firstlineY,words[slsIndex].x+words[slsIndex].width,firstlineY,color,1);
let secondLineY = words[slsIndex+1].y + words[slsIndex+1].height;
graphic.drawLine(words[slsIndex+1].x,secondLineY,lastWords.x+lastWords.width,secondLineY,color,1);
}else{
graphic.drawLine(words[0].x,lineY,lastWords.x+lastWords.width,lineY,color,1);
}
}
var hitRec=HTMLHitRect.create();
hitRec.rec.setTo(words[0].x,lastWords.y,lastWords.x+lastWords.width-words[0].x,lastWords.height);
hitRec.href=this.href;
recList.push(hitRec);
}
}
}
}
if(words[0].y != words[words.length-1].y)
此处的修改。目前这个代码只支持单条富文本的1次换行。如果各位项目有遇到多次换行的,请自行扩展 循环即可.如果同时出现了 点击区域换行bug的话, 也是同理。修改点击区域为多行rect
没有找到相关结果
已邀请:
要回复问题请先登录
3 个回复
ccyy-阿亮
赞同来自:
zengjoeng
赞同来自:
另外官方不知道每天忙什么,基础的东西不解决。
三尺光阴
赞同来自:
workLines(wordList, g, recList) {
var cssStyle = this.style;
var hasLine;
hasLine = cssStyle.textDecoration != "none";
var i = 0, len;
len = wordList.length;
var tStartWord;
tStartWord = wordList[i];
var tEndWord;
tEndWord = tStartWord;
if (!tStartWord)
return;
var tword;
for (i = 1; i < len; i++) {
tword = wordList[i];
if (tword.y != tStartWord.y) {
this.createOneLine(tStartWord, tEndWord, hasLine, g, recList);
tStartWord = tword;
tEndWord = tword;
}
else {
tEndWord = tword;
}
}
this.createOneLine(tStartWord, tEndWord, hasLine, g, recList);
}