문제

나는이 문제로 잠시 동안 고투하고있다 :

HTML이 포함 된 문자열이 있고 정규 표현식을 사용하여 태그간에 문자를 가져옵니다.그런 다음 그런 다음 텍스트 포맷을 그 문자에 적용하려고합니다.

HTML 태그를 표시하지 않으므로 "텍스트"속성 대신 텍스트 필드의 "htmlText"속성을 사용하는 것입니다.따라서 TextFormat을 적용 할 때 정규 표현식에서 반환 된 문자 인덱스가 올바르지 않습니다.

여기에 문제점을 보여주는 몇 가지 샘플 코드가 있습니다.

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);
}
.

이것은 나에게 준다 :

" 샘플 문자열 굵게 표시된 텍스트"

원하는 대신 :

" 샘플 굵은 텍스트가있는 문자열"

match[0].length는 HTML 태그가 너무 길지 않기 때문에 HTML 태그는 <b></b>입니다.

이것에 대해 무엇을 할 수 있습니까?감사합니다!

도움이 되었습니까?

해결책

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.

다른 팁

tf.setTextFormat(format, match.index, match.index + match[1].length); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top