AS3 TextField: Append text at a certain line? - actionscript-3

I'm trying to find a way to append text (appendText) at a certain TextField line number.
I found a way to return the first character of a line:
tf.text.charAt(tf.getLineOffset(10)); //selects line 10
But I haven't found a way to append text. Any help would be appreciated!

This should do the trick (put the supplied text at the start of the supplied line), though there may be a more efficient way of doing it.
function prependToLine(textField:TextField, line:int, text:String):void {
var lineOffset:int = textField.getLineOffset(line-1);
textField.text = textField.text.substring(0,lineOffset) + text + textField.text.substr(lineOffset);
}

Related

How to split line of text (first half bold, second half not bold) when creating document using Google Script

Instead of:
doc.appendparagraph();
how do you add a line of text to your document without it returning to the next line?
I'm trying to break up a line of text and bold the first half the sentence with .setAttributes(bold);
bold which I've defined using....
var boldpl = {};
boldpl[DocumentApp.Attribute.BOLD] = true;.
Thank you.
I've figured out how to do this in a roundabout way using a merge feature, but how do you go about adding a new line instead of a new paragraph? There is no doc.appendText(); feature is there?
Also is there an easy way to make my document single spaced?
You've asked a lot of different questions there!
Given an existing paragraph, how can some portion of it be rendered in
bold while the rest remains normal?
A Document contains a Body, which can contain Paragraphs, each of which can contain other elements, including Text. You can get that Text either as a string - which you can't do much with - or as a Text Object. Here's an example of using the Text.setBold() method to change just some text in a paragraph to bold:
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var firstParagraph = body.getParagraphs()[0];
textElement = firstParagraph.editAsText();
textElement.setBold(0,6,true); // Set the 0th to 6th characters bold
Alternatively, you could use the Text.setAttributes() method with your custom attribute boldpl:
textElement.setAttributes(0,6, boldpl)
Starting with that building block, you could do things like:
Apply bold from the start of a paragraph until the first occurrence of a colon (:).
Apply bold to the first few words.
...or even apply bold to the first half!
textElement.setBold(0,
Math.floor((textElement.getText().length)/2),
true);
There is no doc.appendText(); feature is there?
No... but there is a Paragraph.appendText() method.
Also is there an easy way to make my document single spaced?
You can control the line spacing of text within paragraphs, by setting attributes on individual paragraphs. See Paragraph.setLineSpacing(). Here's a function that sets every paragraph in your document to single-spacing:
function singleSpace() {
var doc = DocumentApp.getActiveDocument();
var bodyElement = DocumentApp.getActiveDocument().getBody();
var paragraphs = bodyElement.getParagraphs();
// Set each paragraph to single-spaced
paragraphs.forEach( function( paragraph ) {
paragraph.setLineSpacing( 1.0 );
});
}

How to lock a character in an input field

I have an input text field in Flash and I want to keep a dollar sign at all times in front of it.
Normally I would just left align the input field and have a "$" next to it so it's uneditable, but in this instance the text field has to be center aligned.
I thought to just include a function to add "$" in front of all the text every time the field loses focus - but realised that would be a problem if there was already a $ sign in front and it just kept adding them.
Also - once I've done this, is there a way to grab the value from that input field excluding the "$"? Eg: some sort of splice that splices the first character and just grabs the rest of it.
To keep the $:
tf.addEventListener( Event.CHANGE, onTextChange );
function onTextChange( e:Event ):void
{
if ( tf.text.charAt(0) != "$" )
tf.text = "$" + tf.text;
}
And to get the text without the first character:
var yourText :String = tf.text.substring(1, tf.text.length);

Detect number of lines required by piece of text

This one is for all the TextField wizards out there. I have a text field that displays 2 paragraphs of texts. The first one I want to be truncated to show only 3 lines of text maximum. The 2nd paragraph is to be appended to that text.
Is there a way I can show only the first 3 lines of the first paragraph? Think of it like an abstract for an article for the first paragraph.
I tried doing it with String.subStr(0, guestimate of 3 lines of chars) but it's pretty inconsistent in that some strings will take up 4 lines of the textfield.
I thought about doing 2 separate textfields but in the case of the first paragraph having only 1 line worth of chars, there will be gap of 2 lines worth of chars between the 2nd paragraph.
textField = new TextField();
textField.antiAliasType = AntiAliasType.ADVANCED;
textField.width = _boundaryWidth;
textField.autoSize = TextFieldAutoSize.NONE;
textField.multiline = true;
textField.wordWrap = true;
textField.text = _text;
textField.setTextFormat(textFormat);
while( textField.numLines > 3 )
{
var txt:String = textField.getLineText(textField.numLines - 1 );
textField.replaceText(textField.text.length - txt.length,textField.text.length, "");
var numLines:int = textField.numLines;
}
This piece of code should do what you are looking for.
All that you need to do is check for the number of lines the text is covering. Set the width initially because that helps the textfield decide how many lines it needs to spread out into. I have not created a function which encompasses all this. But I guess that should be pretty simple to do since the whole function body is here.
Have a look at TextField#getLineOffset(). It will return the character index of the first character on the specified line - you can use substr or substring to truncate the text accordingly.

How to remove a particular text in TextInput in Flash?

Design a Text Input by using components in flash and its InstanceName is listChat. In that text input I added a text by Dynamically. While I am adding text it displays with null.
For Example I added "apple" it Displays like nullapple
How to remove that null?
You can use the String's replace method.
If you want to remove only the first encounter of 'null' you can use this:
listChat.text = listChat.text.replace("null", "");
If you want to remove all encounters of 'null' this will do it:
var stripNullPattern:RegExp = /null/gi;
listChat.text = listChat.text.replace(stripNullPattern, "");
If you only want to remove null if it's in the first four characters use something like this:
if(listChat.text.substring(0, 4) == "null")
{
listChat.text = listChat.text.replace("null", "");
}
Check the AS3 reference for more info:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#replace()

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;
}