Determining text width in a UITextField
I am having problems finding the width of a string of text in a UITextField object. I know there are various methods documented to ostensibly do this, but they simply do not work. There is one and only one method that produces a correct value, but requires the use of the textField property of an mx.controls.Text object. If that textField is used, then getCharBoundaries works correctly, and you can just add up the returned widths from a series of calls to it to get the width of a string of text.
However, if you just call getCharBoundaries on a stand-alone TextField or UITextField (i.e. one that is not a property of MX:Text) then getCharBoundaries returns some entirely different value that is substantially less than if called on the textField property of an Mx:Text object. It is in fact about 25% smaller than the correct value, but that is not even consistent, because change the font size and the discrepancy seems to vary non-linearly. So what getCharBoundaries is actually returning in the case of a stand-alone UITextField, I have no idea.
But actually, call any other method for determining text width of a TextField 开发者_开发百科(other than getCharBoundaries within an MX:Text object) , and you'll get the same erroneous value: TextField.getUITextFormat().measureHTMLText(...).width (or measureText), and TextField.getLineMetrics(...).width all return this same erroneous value. And you might say, "Well they're probably just returning the width from left most pixel in the text to righmost pixel", except as I said to begin with, in one instance getCharBoundaries is actually returning the total cell width of the text, which is what I want, (and then when called from a stand-alone Textfield some completely different value.)
But my problem first appeared when I tried to change an MX:Text object (within which I could call getCharBoundaries successfully) to a UITextField. So now neither getCharBoundaries or anything else works correctly for determining text width. (Note: I'm not actually displaying the UITextfield, but rather am using it for text processing purposes behind the scenes, as it is significantly faster than MX:Text. I set UITextField.htmlText to a value, then make a series of calls to getTextFormat, setTextFormat, and replaceText. And it all works as before with the Mx:Text object, except much faster. Only problem is there is now apparently no way to determine text width for a string of text, even though Adobe documents all these methods that are supposed to do that.
And so my question is, I get on google, and there is very little commentary on this, and no work around at all that I have seen documented anywhere. I did see one thread on actionscript.org from a couple of years ago, where someone else also said that all these width methods simply do not work. There was confirmation of this by someone, but no resolution provided. (Note: this poster noted that the error seemed to escalate with increasing font sizes and was not as noticeable at small font sizes, which I suppose may explain why it doesn't seemed to have been noticed.)
But I downloaded Flex 4 here a while ago, and the problem is still there. So just wondering if I am truly the only person who is cognizant of this, or has encountered it, or possibly someone point out what I'm missing. I guess the Flex Bug tracking system would be an option for me, but that would probably be a Black Hole.
So anyway, just wondering if anyone's encountered this or found a work around.
OK, I've solved my problem. I apologize for the rant against Adobe but then again I might not have solved this otherwise.
The problem was rather obscure and almost certainly not relevant to others, but here it is:
I was trying to find the text width of a prefix that contains some characters in a special nonstandard utility font I created with very nonstandard attributes. Mx:Text.textfield.getCharBoundaries had no problem with it, but every other width method did, evidently. Of course, Mx.Text.textField is not actually a TextField or a UITextField, but rather an IUITextField, meaning it just implements the interface. Evidently its implementation may have nothing in common with the implementation of TextField and UITextField which are both directly related.
As far as this nonstandard font, I had not had any problem with it in the past, so failed to identify it as the culprit. Its character widths are identical to another font's, so that solves my problem.
I have to provide an update here. The problem wasn't the font I was using, but rather that it was not a Windows System Font. measureText, measureHTMLText, etc. won't work unless a font is installed on a user's own system previously. It makes no difference if the font is embedded in the Flex Application, the end user has to already have that font installed on his own system for those width methods to work. MX:Text.textField.getCharBoundaries is the only one that doesn't care whether or not its a systemFont. (UITextField.getCharBoundaries also requires it to be a system font.)
精彩评论