mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
Merge pull request #1904 from RazgrizHsu/NewFeature
Extend the RichText <img> tag's attributes to support height and width
This commit is contained in:
@@ -505,23 +505,38 @@ var RichText = cc.Class({
|
||||
|
||||
var spriteRect = spriteFrame.getRect();
|
||||
var scaleFactor = 1;
|
||||
if(spriteRect.height > this.lineHeight) {
|
||||
scaleFactor = this.lineHeight / spriteRect.height;
|
||||
var spriteWidth = spriteRect.width;
|
||||
var spriteHeight = spriteRect.height;
|
||||
var expectWidth = richTextElement.style.imageWidth;
|
||||
var expectHeight = richTextElement.style.imageHeight;
|
||||
|
||||
//follow the original rule, expectHeight must less then lineHeight
|
||||
if(expectHeight > 0 && expectHeight < this.lineHeight ) {
|
||||
scaleFactor = expectHeight / spriteHeight;
|
||||
spriteWidth = spriteWidth * scaleFactor;
|
||||
spriteHeight = spriteHeight * scaleFactor;
|
||||
} else {
|
||||
scaleFactor = this.lineHeight / spriteHeight;
|
||||
spriteWidth = spriteWidth * scaleFactor;
|
||||
spriteHeight = spriteHeight * scaleFactor;
|
||||
}
|
||||
|
||||
if(expectWidth > 0) spriteWidth = expectWidth;
|
||||
|
||||
if(this.maxWidth > 0) {
|
||||
if(this._lineOffsetX + spriteRect.width * scaleFactor > this.maxWidth) {
|
||||
if(this._lineOffsetX + spriteWidth > this.maxWidth) {
|
||||
this._updateLineInfo();
|
||||
}
|
||||
this._lineOffsetX += spriteRect.width * scaleFactor;
|
||||
this._lineOffsetX += spriteWidth;
|
||||
|
||||
} else {
|
||||
this._lineOffsetX += spriteRect.width * scaleFactor;
|
||||
this._lineOffsetX += spriteWidth;
|
||||
if(this._lineOffsetX > this._labelWidth) {
|
||||
this._labelWidth = this._lineOffsetX;
|
||||
}
|
||||
}
|
||||
this._applySpriteFrame(spriteFrame);
|
||||
sprite.setContentSize(spriteRect.width * scaleFactor, spriteRect.height * scaleFactor);
|
||||
sprite.setContentSize(spriteWidth, spriteHeight);
|
||||
sprite._lineCount = this._lineCount;
|
||||
|
||||
if(richTextElement.style.event) {
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
var eventRegx = /^(click)(\s)*=/;
|
||||
var imageAttrReg = /(\s)*src(\s)*=|(\s)*height(\s)*=|(\s)*width(\s)*=|(\s)*click(\s)*=/;
|
||||
/**
|
||||
* A utils class for parsing HTML texts. The parsed results will be an object array.
|
||||
*/
|
||||
@@ -123,38 +124,46 @@ cc.HtmlTextParser.prototype = {
|
||||
if(header && header[0].length > 0) {
|
||||
tagName = header[0].trim();
|
||||
if(tagName.startsWith("img") && tagName[tagName.length-1] === "/") {
|
||||
var srcBegin = tagName.indexOf('=');
|
||||
var remainingArgument = tagName.substring(srcBegin+1).trim();
|
||||
var imageSrc;
|
||||
var endQuotIndex = -1;
|
||||
header = attribute.match(imageAttrReg);
|
||||
var tagValue;
|
||||
var remainingArgument;
|
||||
var isValidImageTag = false;
|
||||
switch(remainingArgument[0]){
|
||||
case "'":
|
||||
endQuotIndex = remainingArgument.indexOf("'", 1);
|
||||
if(endQuotIndex > -1) {
|
||||
imageSrc = remainingArgument.substring(1, endQuotIndex);
|
||||
isValidImageTag = true;
|
||||
}
|
||||
break;
|
||||
case "\"":
|
||||
endQuotIndex = remainingArgument.indexOf("\"", 1);
|
||||
if(endQuotIndex > -1) {
|
||||
imageSrc = remainingArgument.substring(1, endQuotIndex);
|
||||
isValidImageTag = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
//invalid tag
|
||||
break;
|
||||
}
|
||||
if(isValidImageTag) {
|
||||
obj.isImage = true;
|
||||
obj.src = imageSrc;
|
||||
eventHanlderString = remainingArgument.substring(endQuotIndex+1).trim();
|
||||
if(eventHanlderString.match(eventRegx)){
|
||||
eventObj = this._processEventHandler(eventHanlderString);
|
||||
obj.event = eventObj;
|
||||
while (header) {
|
||||
//skip the invalid tags at first
|
||||
attribute = attribute.substring(attribute.indexOf(header[0]));
|
||||
tagName = attribute.substr(0, header[0].length);
|
||||
//remove space and = character
|
||||
remainingArgument = attribute.substring(tagName.length).trim();
|
||||
nextSpace = remainingArgument.indexOf(' ');
|
||||
|
||||
tagValue = (nextSpace > -1) ? remainingArgument.substr(0, nextSpace) : remainingArgument;
|
||||
tagName = tagName.replace(/[^a-zA-Z]/g, "").trim();
|
||||
tagName = tagName.toLocaleLowerCase();
|
||||
|
||||
attribute = remainingArgument.substring(nextSpace).trim();
|
||||
if (tagName === "src") {
|
||||
obj.isImage = true
|
||||
if( tagValue.endsWith( '\/' ) ) tagValue = tagValue.substring( 0, tagValue.length - 1 );
|
||||
if( tagValue.indexOf('\'')===0 ) {
|
||||
isValidImageTag = true;
|
||||
tagValue = tagValue.substring( 1, tagValue.length - 1 );
|
||||
} else if( tagValue.indexOf('"')===0 ) {
|
||||
isValidImageTag = true;
|
||||
tagValue = tagValue.substring( 1, tagValue.length - 1 );
|
||||
}
|
||||
obj.src = tagValue;
|
||||
} else if (tagName === "height") {
|
||||
obj.imageHeight = parseInt(tagValue);
|
||||
} else if (tagName === "width") {
|
||||
obj.imageWidth = parseInt(tagValue);
|
||||
} else if (tagName === "click") {
|
||||
obj.event = this._processEventHandler(tagName + "=" + tagValue);
|
||||
}
|
||||
header = attribute.match(imageAttrReg);
|
||||
}
|
||||
|
||||
if( isValidImageTag && obj.isImage )
|
||||
{
|
||||
this._resultObjectArray.push({text: "", style: obj});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user