|
|
||||||||
| Ultrashock Tutorials > Flash 8 > Pagination | ||||||||
|
||||||||
|
|
Text Pagination |
|
||||||
IntroductionThere have been many discussions about Flash and its unsuitability for content rich websites, recent releases such as the new online presence for the FWA have attempted to change this. Unlike typical Flash websites that use a scrollbar to display large amounts of text, FWA’s interview and article sections are separated out over multiple pages. This article will show you how to achieve this with dynamic content of any length. You will need to be fairly familiar with ActionScript to continue. TheoryDespite the performance enhancements introduced in Flash 8, text fields containing a large amount of text will prove difficult to scroll efficiently. An appropriate alternative would be to break the text up into a sequence of pages.
Start by creating a new FLA document containing a multi-line dynamic text field with the instance name “textfield”. Then create a new layer with the name “actions”, Lock the layer by clicking the padlock to prevent any visual assets from being placed within it. Select the first key frame of this new layer and open the actions panel (Window > Actions). Below is a detailed explanation of the code that should be entered into this panel. // Assigns the dynamic text field to a variable. // Assigns the text field’s text format to a variable To find the total number of lines that the text will occupy the height of a single line needs to be determined. The depreciated function “getTextExtent” can be used to obtain additional measurement information about the formatted text // Assigns the text extent object to a variable
var ascent = extent.ascent; Now the line height is known we can determine how many lines of text the text field can display at any one time (the number of lines per page). var linesPerPage = field._height / lineHeight; While this equation maybe correct there are a few things that have not been taken into account:All text fields’ have a 2-pixel gap around their perimeter that is not used to hold text-based content. As a result the whole height of the text field should not be used for this calculation but instead the height minus four (two pixels for the top spacing plus two pixels for the bottom spacing). This leaves us with: var linesPerPage = (field._height – 4) / lineHeight; Another important point to realise is that the bottom line of text does not require any leading space between itself and the line below it, as there is no line below it. This is accounted for by the following:var linesPerPage = (field._height + leading – 4) / lineHeight; To complete the equation “Math.floor” will be added to ensure the lines per page is an even number. For instance, if the lines per page came out as 4.8 that would not leave enough space in the text field for five lines of text. The “floor” function always ensures that the number of lines per page is rounded down to the closest integer that is of less or equal value.var linesPerPage = Math.floor((field._height + leading – 4) / lineHeight); The vertical position of the text in a text field can be altered using the “scroll’ property. The scroll property starts from 1 and can be incremented to move through the various lines of text. var lineCount = (field.maxscroll - 1) + linesPerPage; The –1 is to account for the scroll property being one-based. When a text field has a scroll value of 1 the text lies at the very top. For example if the “maxscroll” was equal to two, there would only be a single line that was not visible. (1. when the text is at the top, 2. when the text had been moved down by a single line).As we now know the number of lines per page and the total number of lines we can calculate the total number of pages required to display all the content. var pageCount = Math.ceil(lineCount / linesPerPage); The “ceil” function is used to round the number up to the nearest integer that is of equal or greater value. If the line count divided by the lines per page came out as 3.2, this would be rounded up to 4, as 4 pages are required to display the content. function setPage(page:Number):Void By multiplying the lines per page by the number of pages we can calculate the ideal number of lines we would like our content to span. Then if we subtract the actual number of lines from this value we can determine how many extra lines we will need to add onto our text. var offset = (linesPerPage * pageCount) - lineCount + 1; The +1 is to account for the scroll being one-based.We can now use a “for” loop to add the required number of line breaks. The line break characters we will use depend on the type of the text field. var lineBreak = field.html ? "<br />" : “\r\n”; That concludes all the theory behind this technique, accompanying this article is a source file with a working example of this method in action. DEMO
|
||||||||
©2006 Ultrashock.com - All rights reserved |