[LayaAirIDE 2.0]富文本过程中我遇到的问题以及解决方法(超链接换行以及无法接收超链接鼠标事件以及br或者 标签会崩溃报错的问题)
1. <br/>或者 标签崩溃问题
解决方法 HTMLElemen类中
var words = this._getWords();
if (words == null && (!this._children || this._children.length == 0))
return false;
if (words) {
for (var i = 0, n = words.length; i < n; i++) {
out.push(words[i]);
}
}
if (this._children)
this._children.forEach(function (o, index, array) {
var _style = o._style;
if(_style)
{
_style._enableLayout && _style._enableLayout() && o._addToLayout(out);
}
else
{
o._addToLayout && o._addToLayout(out);
}
});
return true;
}
添加了
if(_style)
{
_style._enableLayout && _style._enableLayout() && o._addToLayout(out);
}
else
{
o._addToLayout && o._addToLayout(out);
} 这个保护
2.富文本超链接无法换行 workLines中索引i没有初始化为0
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);
}
3. 超链接的区域无法支持鼠标事件的mouseover和 mouseout 因此可以扩展一下
第一步 修改此类:
class HTMLHitRect {
constructor() {
this.rec = new Laya.Rectangle();
this.isMouseOver = false;
this.reset();
}
reset() {
this.rec.reset();
this.href = null;
this.isMouseOver = false;
return this;
}
getIsMouseOver()
{
return this.isMouseOver;
}
setIsMouseOver(val)
{
this.isMouseOver = val;
}
recover() {
Laya.Pool.recover("HTMLHitRect", this.reset());
}
static create() {
return Laya.Pool.getItemByClass("HTMLHitRect", HTMLHitRect);
}
}
第二步 class HTMLDivElement 构造中监听一下
this.on(Laya.Event.MOUSE_OVER, this, this._onMouseOver);
this.on(Laya.Event.MOUSE_OUT, this, this._onMouseOut);
this.on(Laya.Event.MOUSE_MOVE, this, this._onMouseMove);
增加如下3个方法
_onMouseOver()
{
var tX = this.mouseX;
var tY = this.mouseY;
var i, len;
var tHit;
len = this._recList.length;
for (i = 0; i < len; i++) {
tHit = this._recList[i];
if (tHit.rec.contains(tX, tY)) {
tHit.setIsMouseOver(true);
this.event("HREF_MOUSE_OVER", [tHit.href]);
}
}
}
_onMouseMove()
{
var tX = this.mouseX;
var tY = this.mouseY;
var i, len;
var tHit;
len = this._recList.length;
for (i = 0; i < len; i++) {
tHit = this._recList[i];
if (tHit.rec.contains(tX, tY)) {
if(!tHit.getIsMouseOver())
{
tHit.setIsMouseOver(true);
this.event("HREF_MOUSE_OVER", [tHit.href]);
}
}
else
{
if(tHit.getIsMouseOver())
{
tHit.setIsMouseOver(false);
this.event("HREF_MOUSE_OUT", [tHit.href]);
}
}
}
}
_onMouseOut()
{
var tX = this.mouseX;
var tY = this.mouseY;
var i, len;
var tHit;
len = this._recList.length;
for (i = 0; i < len; i++) {
tHit = this._recList[i];
if (tHit.getIsMouseOver() && !tHit.rec.contains(tX, tY)) {
tHit.setIsMouseOver(false);
this.event("HREF_MOUSE_OUT", [tHit.href]);
}
}
}
解决方法 HTMLElemen类中
var words = this._getWords();
if (words == null && (!this._children || this._children.length == 0))
return false;
if (words) {
for (var i = 0, n = words.length; i < n; i++) {
out.push(words[i]);
}
}
if (this._children)
this._children.forEach(function (o, index, array) {
var _style = o._style;
if(_style)
{
_style._enableLayout && _style._enableLayout() && o._addToLayout(out);
}
else
{
o._addToLayout && o._addToLayout(out);
}
});
return true;
}
添加了
if(_style)
{
_style._enableLayout && _style._enableLayout() && o._addToLayout(out);
}
else
{
o._addToLayout && o._addToLayout(out);
} 这个保护
2.富文本超链接无法换行 workLines中索引i没有初始化为0
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);
}
3. 超链接的区域无法支持鼠标事件的mouseover和 mouseout 因此可以扩展一下
第一步 修改此类:
class HTMLHitRect {
constructor() {
this.rec = new Laya.Rectangle();
this.isMouseOver = false;
this.reset();
}
reset() {
this.rec.reset();
this.href = null;
this.isMouseOver = false;
return this;
}
getIsMouseOver()
{
return this.isMouseOver;
}
setIsMouseOver(val)
{
this.isMouseOver = val;
}
recover() {
Laya.Pool.recover("HTMLHitRect", this.reset());
}
static create() {
return Laya.Pool.getItemByClass("HTMLHitRect", HTMLHitRect);
}
}
第二步 class HTMLDivElement 构造中监听一下
this.on(Laya.Event.MOUSE_OVER, this, this._onMouseOver);
this.on(Laya.Event.MOUSE_OUT, this, this._onMouseOut);
this.on(Laya.Event.MOUSE_MOVE, this, this._onMouseMove);
增加如下3个方法
_onMouseOver()
{
var tX = this.mouseX;
var tY = this.mouseY;
var i, len;
var tHit;
len = this._recList.length;
for (i = 0; i < len; i++) {
tHit = this._recList[i];
if (tHit.rec.contains(tX, tY)) {
tHit.setIsMouseOver(true);
this.event("HREF_MOUSE_OVER", [tHit.href]);
}
}
}
_onMouseMove()
{
var tX = this.mouseX;
var tY = this.mouseY;
var i, len;
var tHit;
len = this._recList.length;
for (i = 0; i < len; i++) {
tHit = this._recList[i];
if (tHit.rec.contains(tX, tY)) {
if(!tHit.getIsMouseOver())
{
tHit.setIsMouseOver(true);
this.event("HREF_MOUSE_OVER", [tHit.href]);
}
}
else
{
if(tHit.getIsMouseOver())
{
tHit.setIsMouseOver(false);
this.event("HREF_MOUSE_OUT", [tHit.href]);
}
}
}
}
_onMouseOut()
{
var tX = this.mouseX;
var tY = this.mouseY;
var i, len;
var tHit;
len = this._recList.length;
for (i = 0; i < len; i++) {
tHit = this._recList[i];
if (tHit.getIsMouseOver() && !tHit.rec.contains(tX, tY)) {
tHit.setIsMouseOver(false);
this.event("HREF_MOUSE_OUT", [tHit.href]);
}
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
0 个回复