nate451
May 11th, 2009, 12:57 PM
I've developed a relatively simple calendar app in Flash that reads an XML file and displays text based on what day it is. A different bit of text displays in the same text box. Since the text is of varying lengths, I wrote a function to resize the text to be large enough or small enough to fit the box. Here's my function:
// Generic function for increasing text size to fill its container
function AutoTextResize(textElement, maxHeight):void {
textElement.autoSize = TextFieldAutoSize.LEFT;
var textProperties:TextFormat;
var autoTextFormat:TextFormat;
while ( textElement.textHeight < maxHeight ) {
textProperties = textElement.getTextFormat();
autoTextFormat = new TextFormat();
autoTextFormat.size = (textProperties.size + 1);
textElement.setTextFormat(autoTextFormat);
}
// Now that the text size is one greater than necessary, decrease it by one
textProperties = textElement.getTextFormat();
autoTextFormat = new TextFormat();
autoTextFormat.size = (textProperties.size + (-1));
textElement.setTextFormat(autoTextFormat);
trace('Text Height: ' + textElement.textHeight);
trace('Height: ' + textElement.height);
}
The basic idea is: up the text size until it's large enough that the textHeight is larger than the height you pass in (maxHeight), then go back to the size before the one that was too large. This function works great. The textHeight is smaller than the height of the text box.
I have just one bug, and it's utterly confounding me. The text is selectable (which is important: I want users to be able to copy/paste it), and on many of the days (about half), if you select (highlight) the text and drag it to the end, the text shifts up, as if there's blank extra space or a blank extra line at the end. It's as if the field has scrolled to the bottom, even though there's nothing there to scroll to. You can get back to the top by highlighting and dragging toward the beginning.
http://www.kirupa.com/forum/attachment.php?attachmentid=49888&d=1242057355
http://www.kirupa.com/forum/attachment.php?attachmentid=49889&d=1242057363
It behaves as if the textHeight is LARGER than the height of the box, despite the values it outputs in the traces.
I've tried a lot of searching on this problem and can't find anyone that's describing what I'm talking about. If someone could help me with this problem or even help me describe it more accurately, I'd be very grateful.
// Generic function for increasing text size to fill its container
function AutoTextResize(textElement, maxHeight):void {
textElement.autoSize = TextFieldAutoSize.LEFT;
var textProperties:TextFormat;
var autoTextFormat:TextFormat;
while ( textElement.textHeight < maxHeight ) {
textProperties = textElement.getTextFormat();
autoTextFormat = new TextFormat();
autoTextFormat.size = (textProperties.size + 1);
textElement.setTextFormat(autoTextFormat);
}
// Now that the text size is one greater than necessary, decrease it by one
textProperties = textElement.getTextFormat();
autoTextFormat = new TextFormat();
autoTextFormat.size = (textProperties.size + (-1));
textElement.setTextFormat(autoTextFormat);
trace('Text Height: ' + textElement.textHeight);
trace('Height: ' + textElement.height);
}
The basic idea is: up the text size until it's large enough that the textHeight is larger than the height you pass in (maxHeight), then go back to the size before the one that was too large. This function works great. The textHeight is smaller than the height of the text box.
I have just one bug, and it's utterly confounding me. The text is selectable (which is important: I want users to be able to copy/paste it), and on many of the days (about half), if you select (highlight) the text and drag it to the end, the text shifts up, as if there's blank extra space or a blank extra line at the end. It's as if the field has scrolled to the bottom, even though there's nothing there to scroll to. You can get back to the top by highlighting and dragging toward the beginning.
http://www.kirupa.com/forum/attachment.php?attachmentid=49888&d=1242057355
http://www.kirupa.com/forum/attachment.php?attachmentid=49889&d=1242057363
It behaves as if the textHeight is LARGER than the height of the box, despite the values it outputs in the traces.
I've tried a lot of searching on this problem and can't find anyone that's describing what I'm talking about. If someone could help me with this problem or even help me describe it more accurately, I'd be very grateful.