PDA

View Full Version : frustrating character code problem



milkmit
August 11th, 2009, 05:27 PM
I've got a simple function which strips down a textfield to a specific number of lines, and adds "..." at the end, in order to ensure it fits within the textfield of a specific size.



// add caption textfield
var txtCaption:TextField = new TextField();
txtCaption.selectable = false;
txtCaption.mouseEnabled = false;
txtCaption.embedFonts = true;
txtCaption.x = globalSettings.$upperBoxTextFieldStartX;
txtCaption.y = globalSettings.$upperBoxCaptionTextFieldStartY;
txtCaption.width = globalSettings.$upperBoxTextFieldWidth;
txtCaption.height = globalSettings.$upperBoxCaptionTextFieldHeight;
txtCaption.wordWrap = true;
txtCaption.text = FormatText.symbolSweep(loadedData.ENTRY.DATA.A_CAP TION);
txtCaption.setTextFormat(txtFormatCaption); // this is defined above

// make sure it's cut down before adding
if (txtCaption.numLines > 4) {
while (txtCaption.numLines > 4) {
txtCaption.text = txtCaption.text.substr(0, -1); // remove last character one by one until the textfield is 4 lines in length
}
txtCaption.text = txtCaption.text.substr(0, -5); // remove the last 5 characters to make room for the dots
txtCaption.text = txtCaption.text.concat("..."); // add dots
}

addChild(txtCaption);


I've also got another function (in its own class, FormatText.as) which converts the inputted HTML code into a more Flash-friendly unicode (for instance, € becomes \u20AC, which Flash renders properly as the Euro symbol).



public static function symbolSweep(input:String):String {
var r:RegExp = /€/gi;
return input.replace(r, "\u20AC");
}



The problem is that these two things work perfectly fine individually, but don't seem to play right together. When my textfield exceeds the number of lines I'm allowing, it instead shows up empty, both when set to htmlText or just text, whereas it normally clips it just fine if I don't sent it through the symbolSweep.

I realize this is probably a shot in the dark, but does anyone have any clue whatsoever about what might be happening?

creatify
August 11th, 2009, 05:35 PM
I believe what's going on is, when you use subtr on the text content, the string probably included all characters "\u20AC" instead of just one for the symbol itself. I've run into this in the past, the same can go for html tags that aren't visible, though textField.text shouldn't include html tags if I recall. I use a different method for truncating content and adding the ellipses, I'm not sure if this will work w/ the unicode symbol, but may be worth a shot - see my last post in the link, hope this helps:

http://www.kirupa.com/forum/showthread.php?t=332456

milkmit
August 11th, 2009, 05:36 PM
ARGH.. nevermind. It was actually unrelated to the symbolSweep function.

Instead, it looks like I should have removed this from after I set the text:


txtCaption.setTextFormat(txtFormatCaption);


...and used this instead used this BEFORE I set the text:


txtCaption.defaultTextFormat = txtFormatCaption;


That setTextFormat property was left over from earlier, when I was trying to set the textfield as htmlText. I no longer need it here, so setting it as text is just fine.

I can't stand the setTextFormat / defaultTextFormat subtleties when dealing with htmlText/text.

milkmit
August 11th, 2009, 05:43 PM
I believe what's going on is, when you use subtr on the text content, the string probably included all characters "\u20AC" instead of just one for the symbol itself. I've run into this in the past, the same can go for html tags that aren't visible, though textField.text shouldn't include html tags if I recall. I use a different method for truncating content and adding the ellipses, I'm not sure if this will work w/ the unicode symbol, but may be worth a shot - see my last post in the link, hope this helps:

http://www.kirupa.com/forum/showthread.php?t=332456

Ah, thanks, I hadn't realized there were responses to that thread after I posted my little truncate method. It looks like both yours pardoner's methods are a nice (and better in ways) alternates.

Well, as it turns out, I'm pretty sure it wasn't the truncation that was the problem, though I swore it was earlier. I, too, thought Flash might be removing some of the important character code characters, leaving incomplete character codes that break the whole thing....but it doesn't seem that's the case. Since the codes are being converted prior to the truncation, the truncation seems to be treating the replaces characters as they should -- as single characters -- so I'm pretty sure that wasn't doing it.

I'm going to keep testing to be sure, and I'll take a look at your technique to see if it can help me here as well.

Thanks for the reply. Once again, it seems all I needed to do was make a post here in order to figure out how to solve it immediately after. Wish I'd known that hours ago when I started debugging this.... ;)

milkmit
August 11th, 2009, 06:03 PM
Looks like it was a bit more nuanced than I thought.

I'm not sure how or why certain combinations of defaultTextFormat versus setTextformat and htmlText versus text work, but it seems like it craps out when I set to htmlText and *don't* use defaultTextFormat.

I've learned you have to use setTextFormat *after* applying the text to the textfield in order for embedded fonts to work with italics etc, but in this case I (fortunately) don't need those HTML styles, so I'm using defaultTextFormat, which for some reason works. However, setting to htmlText is the only way I can ensure that the whitespace is ignored properly, which is a requirement to force control over the text coming from the database here (the author might make a couple carriage returns, which would ruin this whole little layout, and messes up the truncate function).

Ugh, what a mess... But I think I finally got it working, somehow. :)

creatify
August 11th, 2009, 06:20 PM
Yeah, for the whitespaces, did you try yourTextField.condenseWhite = true; ? that might take care of that the returns etc.

yeah, I have a rule of thumb, if I ever use htmlText = "whatever", that means I'm styling completely with CSS. CSS formatting and textFormat don't play well together at all - not sure if the html output may contain css already injected, but I've hit that with db content to - sounds like you got it figured out though...