Table of Contents

TextField.hscroll Property Flash 6

a text field's horizontal scroll position, in pixels read/write
theField.hscroll

Description

When a field's text is wider or higher than can be accommodated by the physical viewable region of its bounding box, excess text is hidden from view. To bring the text into view, we can scroll the field horizontally or vertically using the hscroll and scroll properties. The integer hscroll property specifies the number of pixels a field's text is scrolled to the left. The default, 0, indicates that the text is not scrolled horizontally (the first character in the field is flush with the left border of the field's bounding box). If a field's text can be fully displayed without any horizontal cutoff, hscroll remains at 0 and cannot be set. But if a field's text cannot be fully displayed horizontally in the allotted space, setting hscroll to a positive integer offsets the text by the specified number of pixels to the left, up to the maximum set by maxhscroll. When more than one line is displayed in a field, all lines are moved simultaneously. Scrolling caused by user input is reflected by hscroll. Note that hscroll does not scroll text one character at a time; it scrolls the field one pixel at a time. This contrasts with scroll, which moves text one line at a time, not one pixel at a time.

The hscroll property typically is used to create horizontal scrolling interfaces for a text field, or for "stock ticker" style text display, as shown in the following Example.

Example

Example 18-5 shows a simple but flexible text ticker that uses the hscroll property to scroll its text horizontally in an endless cycle.

Example 18-5. Text ticker
// Create a text field (with a border so we can see its bounding box)
this.createTextField("ticker_txt", 1, 0, 0, 100, 20);
ticker_txt.border = true;
// Set speed of ticker_txt (pixels per 20 ms)
ticker_txt.speed = 1;
// Assign the field some text for display
ticker_txt.text = "Here is the latest news from moock.org: "
                + "Colin drinks a cup of tea!";
// Assign a buffer of spaces so the text flows in and out of the text field
// First, check the size of a space character in this field's format
ticker_txt.spaceSize = ticker_txt.getNewTextFormat().getTextExtent(" ").width;
// Now calculate how many spaces we need to fill the field
ticker_txt.spacesRequired = Math.ceil(ticker_txt._width / ticker_txt.spaceSize);
// Create a string of spaces
for (var i = 0; i < ticker_txt.spacesRequired; i++) {
  ticker_txt.spacebuffer += " ";
}
// Add the spaces to the front and end of the text
ticker_txt.text = ticker_txt.spacebuffer + ticker_txt.text + ticker_txt.spacebuffer;
// Create a function to scroll the text
ticker_txt.hscrollInterval = function () {
  // If we're at the end of the text...
  if (this.hscroll =  = this.maxhscroll) {
    // ...start again
    this.hscroll = 0;
  }
  // Scroll the text by the specified amount
  this.hscroll += this.speed;
}
// Call our text scroller every 20 milliseconds
setInterval(ticker_txt, "hscrollInterval", 20);

See Also

setInterval( ), TextField.getNewTextFormat( ), TextField.maxhscroll, TextField.scroll, TextField.width, TextField.wordWrap, TextField._yscale, TextFormat.getTextExtent( ); the FScrollBar component in Appendix G


Table of Contents