Detect number of lines required by piece of text - actionscript-3

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.

Related

How to set a certain number of spaces or indents before a Paragraph in Google Docs using Google Apps Script

I have a 20 line script, and I want to make sure that each paragraph is indented exactly once.
function myFunction() {
/*
This function turns the document's format into standard MLA.
*/
var body = DocumentApp.getActiveDocument().getBody();
body.setFontSize(12); // Set the font size of the contents of the documents to 9
body.setForegroundColor('#000000');
body.setFontFamily("Times New Roman");
// Loops through paragraphs in body and sets each to double spaced
var paragraphs = body.getParagraphs();
for (var i = 3; i < paragraphs.length; i++) { // Starts at 3 to exclude first 4 developer-made paragraphs
var paragraph = paragraphs[i];
paragraph.setLineSpacing(2);
// Left align the first cell.
paragraph.setAlignment(DocumentApp.HorizontalAlignment.LEFT);
// One indent
paragraph.editAsText().insertText(0, "\t"); // Adds one tab every time
}
var bodyText = body.editAsText();
bodyText.insertText(0, 'February 3, 1976\nMrs. Smith\nYour Name Here\nSocial Studies\n');
bodyText.setBold(false);
}
The code I have tried doesn't work. But my expected results are that for every paragraph in the for loop in myFunction(), there are exactly 4 spaces before the first word in each paragraph.
Here is a sample: https://docs.google.com/document/d/1sMztzhOehzheRdqumC6PLnvk4qJgUCSE0irjTZ0FjTQ/edit?usp=sharing
If the user uses Autoformat, but already has the paragraphs indented...
Update
I have investigated use of the Paragraph.setIndentFirstLine() method. When I set it to four, it sets it to 1 space. Now I realize this is because points and spaces are not the same thing. What number do I need to multiply by to get four spaces in points?
Let us consider a few basic identing operations: manual and by script.
The following image shows how to indent current paragraph (cursor stays inside this one).
Please note, the units are centimetres. Also note, that the paragraph does not include leading spaces or tabs, we have no need of them.
Suppose we would like to get the indent values in the script and apply them to the next paragraph. Look at the code below:
function myFunction() {
var ps = DocumentApp.getActiveDocument().getBody().getParagraphs();
// We work with the 5-th and 6-th paragraphs indeed
var iFirst = ps[5].getIndentFirstLine();
var iStart = ps[5].getIndentStart();
var iEnd = ps[5].getIndentEnd();
Logger.log([iFirst, iStart, iEnd]);
ps[6].setIndentFirstLine(iFirst);
ps[6].setIndentStart(iStart);
ps[6].setIndentEnd(iEnd);
}
If you run and look at the log, you will see something like this: [92.69291338582678, 64.34645669291339, 14.173228346456694]. No surprise, we have typographic points instead of centimetres. (1cm=28.3465pt) So we can measure and modify any paragraph indent values precisely.
Addition
For some reasons you might want to control spaces number at the beginning of the paragraph. It is also possible by scripting, but it has no effect on the paragraph's "left" or "right" indents.
Sample code below is for similar task: count leading spaces number of the 5-th paragraph and make the same number of spaces at the beginning of the next one.
function mySpaces() {
var ps = DocumentApp.getActiveDocument().getBody().getParagraphs();
// We work with the 5-th and 6-th paragraphs indeed
var spacesCount = getLeadingSpacesCount(ps[5]);
Logger.log(spacesCount);
var diff = getLeadingSpacesCount(ps[6]) - spacesCount;
if (diff > 0) {
ps[6].editAsText().deleteText(0, diff - 1);
} else if (diff < 0) {
var s = Array(1 - diff).join(' ');
ps[6].editAsText().insertText(0, s);
}
}
function getLeadingSpacesCount(p) {
var found = p.findText("^ +");
return found ? found.getEndOffsetInclusive() + 1 : 0;
}
We have used methods deleteText() and insertText() of the class Text for proper corrections and findText() to locate the spaces if any. Note, the last method argument is a string, representing a regular expression. It matches "all leading spaces", if they exist. See more details about regular expression syntax.

add textfield from an array plus simple text,to another textfield of an array

I have two arrays with some textfields inside them.What i want to do is to put the text of textfield of the first array plus some simple text into the second array textfield.
Example:
var array1:Array=new Array();
var array2:Array=new Array();
var mytext1:TextField=new TextField();
var mytext2:TextField=new TextField();
mytext2.text="goodbye";
array1[0]=mytext1;
array2[0]=mytext2;
array1[0].text=array2[0].text+" hello";
trace(array1[0].text);
I believe this would trace:
goodbye hello
But instead it traces them in different rows:
goodbye
hello
You will need te either make the text field bigger, or auto size it
easiest is to just set the width
mytext1.width = 350;
you can also adjust these parameters
mytext1.border = true;
mytext1.wordWrap = false;
to see the border, and to prevent folding line
you can look up more about text fields and how to autosize here: http://www.republicofcode.com/tutorials/flash/as3text/

AS3 TextField: Append text at a certain line?

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

split data(Text) of a text field into two strings, and use each string on different section of a visualforce page

I have an account with a Note in notesandattachment related list.
I want to display the text of 8 lines, out of 50 lines on a section of visualforce page.
And remaining 42 lines in another section of same visualforce page.
what i know :
I need to split the notes.body into two substrings. One, with 8 lines of text and second one with 42 line.I need to use div tags to get this done on the visualforce page. I might be wrong.
Please suggest me, how do i achieve this.
Appreciate the help.
You can split the text in controller by using split() method like this
String allLines[] = allText.split('\n');
String first8Lines = '';
String restLines ='';
for(Integer i =0; i<8 ; i++){
first8Lines += allLines[i];
}
for(Integer i =7; i<50 ; i++){
restLines += allLines[i];
}
And then put {!first8Lines} in one place and {!restLines} in another

How to break a text at 80 chars precisely?

After having asked this question in the DB section and being adived to ask it here, here is my problem:
I have the problem, that I have a long text that is being read from a database. The text itself is just a one liner. The problem is that it needs to be broken at exactly 80 chars even when in the middle of a word.
HTML or other languages will make the line break if the next word doesn't fit in the remaining chars and that is not what I want. The pages are done in jsf.
For example:
textarea= cols: 8 rows: 3
input= break these texts
normal:
break
these
texts
what I need:
break th
ese text
s
Any ideas on how I can do this?
you can use the below function just pass the string (you want to brake after every 80 char) to the function
function breakText(str)
{
i=0;
outputStr="";
str= str.replace(/(\r\n|\n|\r)/g," ");
while(i<str.length)
{
outputStr += str.substr(i,80) +"<br>"; // replace br with any line break you want
i=i+80;
}
return outputStr
}
NOTE : this function will replace all the line break and insert a <br> after every 80 char
Working DEMO