如何从 JSFL 中的 TextField 获取 HTML 格式字符串
Posted
技术标签:
【中文标题】如何从 JSFL 中的 TextField 获取 HTML 格式字符串【英文标题】:How to get the HTML format string from a TextField in JSFL 【发布时间】:2013-01-04 17:36:55 【问题描述】:如何访问时间轴上 TextField 的 *.htmlText 属性?我正在寻找可以返回所有格式信息的东西,就像它在运行时在 ActionScript 3.0 中所做的那样。
例子:
<TEXTFORMAT LEADING="2">
<P ALIGN="CENTER">
<FONT FACE="Verdana"
SIZE="64"
COLOR="#FF0000"
LETTERSPACING="0"
KERNING="1">
<B>This is a </B>
<FONT COLOR="#000000">
<B>bold</B>
<FONT SIZE="33">
<B>example</B>
</FONT>
</FONT>
</FONT>
</P>
</TEXTFORMAT>
【问题讨论】:
想出一个解决方案......再一次!完成后将发布解决方案。 【参考方案1】:必须从头开始写!所以这里是:
/*
Usage:
var labelHTML = HTMLUtils.convertToHTML( someLabel );
trace("The HTML of the selected label in the IDE is:\n" + labelHTML);
*/
var _TAG_TEMPLATE = "<$0$1>$2</$0>";
HTMLUtils =
convertToHTML: function(pTextField)
var runs = pTextField.textRuns,
run, content, output,
leading;
this.rootNode = new HTMLElement("ROOT");
for (var r=0, rLen=runs.length; r<rLen; r++)
run = runs[r];
content = run.characters;
this.convertAttrsToHTML(run.textAttrs, content);
this.currentTextFormat = null;
return this.rootNode.toHTML(true);
,
convertAttrsToHTML: function(pTextAttrs, pContent)
var contentLines = pContent.split("\r");
var masterFontNode;
if(!this.currentTextFormat)
masterFontNode = this.createNewTextFormat( pTextAttrs );
else
masterFontNode = this.currentTextFormat.childAt(0,0);
var fontNode = new HTMLFont();
fontNode.addNode( new HTMLText( pContent ) );
this.assignFontAttributes( fontNode, pTextAttrs );
masterFontNode.addNode( fontNode );
//trace( pTextAttrs.toTrace() );
this.currentTextFormat.attributes.leading = String(pTextAttrs.lineSpacing);
this.currentTextFormat.children[0].attributes.align = String(pTextAttrs.alignment);
if(contentLines.length>1)
this.currentTextFormat = null; //
,
createNewTextFormat: function( pTextAttrs )
this.currentTextFormat = new HTMLElement("TEXTFORMAT");
this.rootNode.addNode(this.currentTextFormat);
var paragraph = new HTMLElement("P");
this.currentTextFormat.addNode( paragraph );
var fontNode = new HTMLFont();
paragraph.addNode( fontNode );
this.assignFontAttributes( fontNode, pTextAttrs );
return fontNode;
,
assignFontAttributes: function( pFontNode, pTextAttrs )
pFontNode.attributes.face = String(pTextAttrs.face);
pFontNode.attributes.size = String(pTextAttrs.size);
pFontNode.attributes.letterSpacing = String(pTextAttrs.letterSpacing);
pFontNode.attributes.color = String(pTextAttrs.fillColor);
pFontNode.isBold = pTextAttrs.bold;
pFontNode.isItalic = pTextAttrs.italic;
;
HTMLElement = Class.extend(
init: function( pName )
this.name = pName;
this.children = [];
this.parent = null;
this.attributes = ;
,
clone: function(pOnlyThis)
var theClone = new HTMLElement( this.name );
theClone.attributes = this.attributes.copy();
return theClone;
,
addNode: function(pNode)
this.children.push(pNode);
pNode.parent = this;
return pNode;
,
childAt: function()
var current = this;
for (var a=0, aLen=arguments.length; a<aLen; a++)
var index = arguments[a];
current = current.children[index];
return current;
,
parentOfType: function(pName)
var currentNode = this.parent;
while(currentNode && currentNode.name!=pName)
currentNode = currentNode.parent;
return currentNode;
,
childrenHTML: function()
var theHTML = "";
var theChildren = this.children,
theChild;
for (var c=0, cLen=theChildren.length; c<cLen; c++)
theChild = theChildren[c];
theHTML += theChild.toHTML();
return theHTML;
,
toHTML: function(pInnerOnly)
var theHTML = this.childrenHTML();
if(pInnerOnly)
return theHTML;
var theAttributes = [];
var theAttrProperties = this.attributes.getProperties();
for(var a=0, aLen=theAttrProperties.length; a<aLen; a++)
var attr = theAttrProperties[a];
var attrBIG = attr.toUpperCase();
var attrValue = this.attributes[attr];
theAttributes.push(attrBIG + "=\"" + attrValue + "\"");
if(theAttributes.length==0)
theAttributes = "";
else
theAttributes = " " + theAttributes.join(" ");
return _TAG_TEMPLATE.inject(this.name, theAttributes, theHTML);
);
HTMLFont = HTMLElement.extend(
init: function()
this._super("FONT");
,
toHTML: function(pInnerOnly)
var parentFont = this.parentOfType("FONT");
if(parentFont)
//Find differences in attributes:
var parentAttrs = parentFont.attributes;
var myAttrs = this.attributes;
var theAttrProperties = myAttrs.getProperties();
var differentAttrs = [];
for (var a=0, aLen=theAttrProperties.length; a<aLen; a++)
var attr = theAttrProperties[a];
var attrValue = myAttrs[attr];
var parentValue = parentAttrs[attr];
if(parentValue==null || parentValue==attrValue)
continue;
differentAttrs.push( attr.toUpperCase() + "=\"" + attrValue + "\"");
var theHTML = this.childrenHTML();
if(this.isBold) theHTML = "<B>" + theHTML + "</B>";
if(this.isItalic) theHTML = "<I>" + theHTML + "</I>";
if(differentAttrs.length==0)
return theHTML;
else
differentAttrs = " " + differentAttrs.join(" ");
return _TAG_TEMPLATE.inject(this.name, differentAttrs, theHTML);
return this._super(pInnerOnly);
);
HTMLText = HTMLElement.extend(
init: function( pContent )
this._super("TEXT");
this._content = pContent;
,
toHTML: function()
return this._content;
);
注意:对于定义类和扩展它们的部分,您可以从该站点获得该功能:@ 987654321@。这是一个很棒的脚本,可以作为任何基于 javascript 的语言的核心,它极大地简化了 OOP!
【讨论】:
可能有一些我忘记包含的依赖项,但基本上对于任何对象迭代(例如:通过'attributes'属性),在JavaScript中你可以简单地做一个for-in循环,其中keys 将是属性名称,object[key] 是值。 您是否在 Java 中找到了具有相同功能的东西? 我已经有一段时间没有接触过这段代码了/更不用说在 Flash 和 Java 中处理 HTML 文本了 :( 抱歉,不知道是否存在类似的解决方案。以上是关于如何从 JSFL 中的 TextField 获取 HTML 格式字符串的主要内容,如果未能解决你的问题,请参考以下文章
将Classic Textfield动态转换为TFL Textfield?