Modify TLF Text Field properties that is on stage - actionscript-3

I have a TLF Text field on the stage. I am trying to test this out in simple flash document.
My code takes in some xml that I parse. The xml will vary and will not always change all the properties of the text field. For instance in one case I only want to change the size of the font. In another case I only want to change the alignment of the font.
I am using TLF Text fields because we will translating into Arabic and I already have gotten Right to Left text working with them.
These are some properties I will need to edit in code:
Font Size
Font
Alignment
Leading
Bold, Italic, Underline (weight)
Any coding help would be great. I have seen ideas out there for text flow and text layout but I am obviously not using it correctly because I can't get it to work.

Long ago before I give up and stop using TLF fields. I have a project that requests dynamic addind and removing tlf fileds from/to stage. This is a code from this project:
This will generate default format dynamically
var config:Configuration = new Configuration();
var defTextFormat: TextLayoutFormat = new TextLayoutFormat();
defTextFormat.textAlign = TextAlign.LEFT;
defTextFormat.fontFamily = m_strFontName;
defTextFormat.fontSize = m_nFontSize;
defTextFormat.fontWeight = FontWeight.BOLD
defTextFormat.paddingLeft = 3;
defTextFormat.paddingTop = 3;
defTextFormat.paragraphStartIndent = 3;
defTextFormat.paragraphSpaceBefore = 3;
config.defaultLinkActiveFormat = defTextFormat;
config.defaultLinkHoverFormat = defTextFormat;
config.defaultLinkNormalFormat = defTextFormat;
config.textFlowInitialFormat = ITextLayoutFormat( defTextFormat );
m_textFlow = new TextFlow( config );
member m_textFlow holds a ref to TLF field.
To add and remove elements use m_textFlow.addChild( p ); where p is paragraph element
see: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flashx/textLayout/elements/ParagraphElement.html
To change the FontSize and color an element for example :
var _p:ParagraphElement = ParagraphElement( m_textFlow.getChildAt( iChild ) );
for ( var iParChild: uint = 0; iParChild < _p.numChildren; ++iParChild )
{
_p.getChildAt( iParChild ).color = color;
_p.getChildAt( iParChild ).fontSize = nRatio;
...
Maybe this can help you.

Related

Centering in a gDoc after a replace

I am looking to replace a string within a Google Doc via an app script. The string will exist on a line, but after the replace, I want it to have a specific font, size and justification.
I've created a style to address all these attributes (I included both Horiz. and Vert. alignment) and most of it works fine. When the string is replaced, the replacement has the right font, size and bold attributes. For some reason, I cannot get the justification to get changed.
// Define the style for the replacement string.
var hdrStyle = {};
hdrStyle[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
DocumentApp.HorizontalAlignment.CENTER;
hdrStyle[DocumentApp.Attribute.VERTICAL_ALIGNMENT] =
DocumentApp.VerticalAlignment.CENTER;
hdrStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
hdrStyle[DocumentApp.Attribute.FONT_SIZE] = 24;
hdrStyle[DocumentApp.Attribute.BOLD] = true;
{ then later }
documentBody = DocumentApp.openById(fileId).getBody();
hdrElem = documentBody.findText("old string").getElement();
hdrText = hdrElem.asText().setText("new string");
// Force our 'header style':
hdrElem.setAttributes(hdrStyle);
I've tried setting the style after the findText and (as here) after, but no change in centering.
I see there is a paragraph centering, but I am not clear how to 'get' the paragraph associated with the element that is returned on the find.
I'm hoping this is some simple set of calls - but have run out of ideas (and patience)..!
Any help would be appreciated!
You can use getParent() on hdrElem to get the parent paragraph to apply the styling to.
https://developers.google.com/apps-script/reference/document/text#getParent()
documentBody = DocumentApp.openById(fileId).getBody();
hdrElem = documentBody.findText("old string").getElement();
hdrText = hdrElem.asText().setText("new string");
var hdrParent = hdrElem.getParent()
// Force our 'header style':
hdrParent.setAttributes(hdrStyle);

tlfTextField - Highlight a part of text with "Code"

I wonder how to set the text "Highlight" of a part of text inside tlfTextField with the code?
I tried "tf.backgroundColor = 0x990000" property, but did not help.
For instance, I can change the Font Color of any contents inside Parenthesis, by this code:
private function decorate():void {
var tf:TextFormat = new TextFormat();
tf.color = 0x990000;
var startPoint:int = 0;
while (startPoint != -1) {
var n1:int = textMc.tlfText.text.indexOf("(", startPoint);
var n2:int = textMc.tlfText.text.indexOf(")", n1 + 1);
if (n1 == -1 || n2 == -1) {
return;
}
textMc.tlfText.setTextFormat(tf, n1 + 1, n2);
startPoint = n2 + 1;
}
}
So I know "tf.color = 0x990000;" will change the Font color, however, don't know how to "highlight" some text, with code, as I do inside Flash manually.
You should have probably used tlfMarkup property to set the required format to the specific part of text. The attributes you seek are backgroundColor and backgroundAlpha of the span XML element that you should wrap your selection, however it should be much more difficult should there already be spans around words when you retrieve the property from your text field.
The problem with your solution is that you don't check if the two characters are located on a single line before drawing your rectangle, also you would need to redraw such rectangles each time something happens with the textfield. The proposed approach makes use of Flash HTML renderer's capabilities to preserve the formatting, however it will require a lot of work to handle this task properly.

How to show html style text in excel cell with SpreadsheetGear

I get a string that contains html content, like this:
"i am headhello"
now, i want to write this string into excel cell, and let these html tags render the bold style.
How can i do this with spreadsheetgear?
SpreadsheetGear does not support parsing and rendering HTML. If you put this type of content in a cell, the raw markup will be displayed instead.
SpreadsheetGear does support adding rich-text (RTF) to a cell, but you would need to do this with SpreadsheetGear API using:
IRange.GetCharacters(...)
ICharacters interface
The following example code would render something similar to this:
// Create new workbook.
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.ActiveWorksheet;
IRange cells = worksheet.Cells;
// Add text to A1 which we'll format below...
cells["A1"].Value = "This Is My Header\n\nHello World!";
// Format "header" as bold and with a larger font size.
ICharacters charsHeader = cells["A1"].GetCharacters(0, 17);
charsHeader.Font.Bold = true;
charsHeader.Font.Size = 18;
// Format "Hello" text.
ICharacters charsHello = cells["A1"].GetCharacters(19, 5);
charsHello.Font.Italic = true;
charsHello.Font.Color = SpreadsheetGear.Colors.DarkRed;
// Format "World" text.
ICharacters charsWorld = cells["A1"].GetCharacters(25, 5);
charsWorld.Font.Underline = UnderlineStyle.Single;
charsWorld.Font.Color = SpreadsheetGear.Colors.DarkBlue;
// Expand column width to accommodate header text
cells["A:A"].ColumnWidth = 30;
// Save and view in Excel...
workbook.SaveAs(#"c:\temp\rtf.xlsx", FileFormat.OpenXMLWorkbook);
// ...or attach to SpreadsheetGear's WPF WorkbookView to
// confirm RTF is displaying as expected (NOTE: the WinForms
// WorkbookView does not support rendering RTF).
workbookView.ActiveWorkbook = workbook;

How do I change the color of a part of a word with actionscript?

I'm working on an exercise where children have to drag a textfield to the correct box.
The textfield contains a word I loaded from an xml.
When they drop the textfield in the correct box, I want to change the color of a part of the text in the textfield.
This always has to be a specific letter / combination of letters.
For instance: they have to learn the difference between e and ee. If they drop a textfield with the word "ten" in the correct box, I want the "t" and the "n" to remain black, while the "e" needs to become blue.
But if they drop the word "tween" in the correct box, the letters "ee" need to become blue.
I have been toying with setting the textFormat of a part of a string, but I seem to be unable to get this working. It's also hard because the number of characters of both options (e and ee) differ.
But I am not getting anywhere.
tldr; I want to change the color of a part of the text in the textfield
How about :
//setup a textformat
var textFormat:TextFormat = new TextFormat();
var startIndex:int = someString.indexOf("e");
textField.setTextFormat(textFormat,startIndex,startIndex+1);
To change the color of some characters in the middle of a string use html:
http://cartoonsmartblog.wordpress.com/2009/08/28/coloring-html-text-in-flash-actionscript-3-quick-code-note/
(btw just use single quotes and double quotes instead of that \u0022 ugliness)
I've come to the conclusion the best way to change the colour of a part of a word is to use the Stylesheet class. I took the snippet below from someone here on Stackoverflow but I forgot to note who it was. I'm sorry, whoever you are.
var style:StyleSheet = new StyleSheet();
var styleObj:Object = new Object();
styleObj.fontSize = "bold";
styleObj.color = "#FF0000";
style.setStyle(".red",styleObj);
var tf:TextField = new TextField();
tf.styleSheet = style;
tf.htmlText = "fox is <span class='red'>red</span>";
addChild(tf);

JSFL: convert text from a textfield to a HTML-format string

I've got a deceptively simple question: how can I get the text from a text field AND include the formatting? Going through the usual docs I found out it is possible to get the text only. It is also possible to get the text formatting, but this only works if the entire text field uses only one kind of formatting. I need the precise formatting so that I convert it to a string with html-tags.
Personally I need this so I can pass it to a custom-made text field component that uses HTML for formatting. But it could also be used to simply export the contents of any text field to any other format. This could be of interest to others out there, too.
Looking for a solution elsewhere I found this:
http://labs.thesedays.com/blog/2010/03/18/jsfl-rich-text/
Which seems to do the reverse of what I need, convert HTML to Flash Text. My own attempts to reverse this have not been successful thus far. Maybe someone else sees an easy way to reverse this that I’m missing? There might also be other solutions. One might be to get the EXACT data of the text field, which should include formatting tags of some kind(XML, when looking into the contents of the stored FLA file). Then remove/convert those tags. But I have no idea how to do this, if at all possible. Another option is to cycle through every character using start- and endIndex, and storing each formatting kind in an array. Then I could apply the formatting to each character. But this will result in excess tags. Especially for hyperlinks! So can anybody help me with this?
A bit late to the party but the following function takes a JSFL static text element as input and returns a HTML string (using the Flash-friendly <font> tag) based on the styles found it its TextRuns array. It's doing a bit of basic regex to clear up some tags and double spaces etc. and convert /r and /n to <br/> tags. It's probably not perfect but hopefully you can see what's going on easy enough to change or fix it.
function tfToHTML(p_tf)
{
var textRuns = p_tf.textRuns;
var html = "";
for ( var i=0; i<textRuns.length; i++ )
{
var textRun = textRuns[i];
var chars = textRun.characters;
chars = chars.replace(/\n/g,"<br/>");
chars = chars.replace(/\r/g,"<br/>");
chars = chars.replace(/ /g," ");
chars = chars.replace(/. <br\/>/g,".<br/>");
var attrs = textRun.textAttrs;
var font = attrs.face;
var size = attrs.size;
var bold = attrs.bold;
var italic = attrs.italic;
var colour = attrs.fillColor;
if ( bold )
{
chars = "<b>"+chars+"</b>";
}
if ( italic )
{
chars = "<i>"+chars+"</i>";
}
chars = "<font size=\""+size+"\" face=\""+font+"\" color=\""+colour+"\">"+chars+"</font>";
html += chars;
}
return html;
}