Domanda

Ho lottato con questo problema per un po ':

Ho una stringa contenente html e sto usando un'espressione regolare per ottenere i caratteri tra i tag.Allora tentando di applicare un textFormat a quei caratteri.

Il problema è che sto usando la proprietà "htmltext" di Textfield invece della sua proprietà "testo" (perché non voglio che i tag HTML siano visibili).Quindi, l'indice dei personaggi restituito dall'espressione regolare non è corretta, quando applichisco il TextFormat.

Ecco alcuni codice di esempio che illustra il problema:

var txt:String = "<b>Sample</b> string with bold text";

var tf:TextField = new TextField();
addChild(tf);
tf.htmlText = txt;

var format:TextFormat = new TextFormat();
format.bold = true;

var regExp:RegExp = /<b>(.*?)<\/b>/g;
var match:Object = regExp.exec(txt);
while (match != null) {
    tf.setTextFormat(format, match.index, match.index + match[0].length);
    match = regExp.exec(txt);
}
.

Questo mi dà:

" String sample con testo in grassetto"

invece del desiderato:

" Sample String con testo grassetto"

Poiché match[0].length è di sette caratteri troppo a lungo, a causa dei tag HTML <b></b>.

Cosa posso fare su questo?Grazie!

È stato utile?

Soluzione

Using TextField.htmlText, <b> tags should give bold text, without any need for TextFormat.bold or regexp, provided you embed the right fonts (or use device fonts).

But, I know there is sometimes issues with the HTML support in TextFields in combination with font handling, and perhaps your actual situation is more complex than the example with bold text. In that case I would recommend using StyleSheet formatting instead of the regexp/TextFormat combo. Besides the problem with offset mismatch that you have encountered, I believe that combining the two different approaches to text formatting - HTML and TextFormat - runs the risk of giving other problems, while using HTML text and StyleSheet are meant to be used together.

I started writing an example of using StyleSheet/htmlText, but since <b> should work anyway, without styling, it got a bit weird, so I scratched it. But let me know if you need example code.

Altri suggerimenti

tf.setTextFormat(format, match.index, match.index + match[1].length); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top