0

I'm having an issue with applying textformat to various parts of a string:

feedBackText = "This is <b>bold</b>, and this is some more text, and <b>this is bold too</b>. But this is not bold. This is <b>bold</b>!";

feedbackTextField.htmlText = feedBackText;

var startBoldPos:int = 0;
var closeBoldPos:int = 0;
var i:uint = 0;

while(true) {

startBoldPos = feedBackText.indexOf("<b>", startBoldPos);
closeBoldPos = feedBackText.indexOf("</b>", startBoldPos);

if(startBoldPos > 0) {
    i++;
// Here is the main trouble:
    feedbackTextField.setTextFormat(_boldFormat, startBoldPos-((7)*i), closeBoldPos-((10)*i));
    trace("i is: " + i);
    trace("Feedbacktext: " + feedBackText);
    trace("Start bold: " + startBoldPos);
    trace("End bold: " + closeBoldPos + "\n");
} else {
// This works as expected
    feedbackTextField.setTextFormat(_boldFormat, startBoldPos, closeBoldPos-3);
    //  trace("Feedbacktext: " + feedBackText);
    //  trace("Start bold: " + startBoldPos);
    //  trace("End bold: " + closeBoldPos + "\n");
}       
if(startBoldPos == -1) break;
startBoldPos = closeBoldPos;
}

I'm trying to play around with the index of where the setTextFormat should be assigned, but it doesn't seem to align with startBoldPos and endBoldPos. Even if the traces are showing the correct numbers of where to place setTextFormat it in the string.

Any ideas would be apppreciated!

Regards, Hans Magnus

1 Answers1

0

I tested your code and it works as expected. I don't fully understand what you trying to do, so here some general remarks:

  1. You can set some formatting without setTextFormat only with htmlText. After assigning text with html tags textField text will be already partly formatted.

  2. setTextFormat works with text property, so start and end index calculated depend on text without html tags. In your case it will be: This is bold, and this is some more text, and this is bold too. But this is not bold. This is bold!

And tracing your code step by step:

1) Setting text with to htmlText property in TextField. After this TextField contained:

This is bold, and this is some more text, and this is bold too. But this is not bold. This is bold!

2) Loop starts. First iteration:

startBoldPos-((7)*i) = 1
closeBoldPos-((10)*i) = 5

TextField: This is bold, and this is some more text, and this is bold too. But this is not bold. This is bold!

3) Second iteration:

startBoldPos-((7)*i) = 39
closeBoldPos-((10)*i) = 52

TextField: This is bold, and this is some more text, and this is bold too. But this is not bold. This is bold!

4) Third iteration:

startBoldPos-((7)*i) = 87
closeBoldPos-((10)*i) = 85

Nothing changed bacause endPosition < startPosition.

5) Fourth iteration:

startBoldPos = -1
closeBoldPos-3 = 12

TextField: This is bold, and this is some more text, and this is bold too. But this is not bold. This is bold!

And final result on screenshot:enter image description here

UPDATE (formatting without setTextFormat method):

...
[Embed(source="GOTHIC.TTF", fontName="Gothic", embedAsCFF="false", advancedAntiAliasing="true")]
private var gothicFont:Class;

[Embed(source="GOTHICB.TTF", fontName="Gothic", embedAsCFF="false", advancedAntiAliasing="true", fontWeight="bold")]
private var gothicFontBold:Class;   
...
var feedBackText:String = "This is <b>bold</b>, and this is some more text, and <b>this is bold too</b>. But this is not bold. This is <b>bold</b>!";
var feedbackTextField:TextField = new TextField();
feedbackTextField.defaultTextFormat = new TextFormat("Gothic", 14);
feedbackTextField.embedFonts = true;
feedbackTextField.width = 500;
feedbackTextField.htmlText = feedBackText;

And result (with embed font as you see): enter image description here

Crabar
  • 1,829
  • 1
  • 14
  • 26
  • Thanks @crabar ! The code is used in a class which has the fonts Myriad Pro and Myriad Pro Bold imported, and the TextField feedbackTextField is set as `embedFonts = true;` If I use htmlText only, the bolds are not formatted, as it seems I need to use TextFormat to do this with this font embedded? The line `feedbackTextField.setTextFormat(_boldFormat, startBoldPos-((7)*i), closeBoldPos-((10)*i));` needs to find the `` and `` tags in the string, and format the text accordingly. Traced positions looks correct, but the bolding is wrong compared to the given string in line 1. – Hans Magnus Aug 03 '14 at 07:10
  • Could you explain what the problem do you have now? You are using embed font and string after formatting is not a bold? Or it bolds on incorrect positions? Or string is invisible after formatting? – Crabar Aug 03 '14 at 08:42
  • The bolds are applied in the wrong places, so there is something wrong with the way I use the index in `setTextFormat`. Even if the traced values of `startBoldPos` and `closeBoldPos`seem correct, there is something wrong with `feedbackTextField.setTextFormat(boldFormat, startBoldPos, closeBoldPos);` if there is more than one set of `` and ``tags in the string. The bolding shows up, but at the wrong places... – Hans Magnus Aug 03 '14 at 08:52
  • Could you show what string you expected and want string you have now? In my answer I found that something wrong with calculations and show what string you have after this formatting and why. – Crabar Aug 03 '14 at 08:56
  • "If I use htmlText only, the bolds are not formatted, as it seems I need to use TextFormat to do this with this font embedded?" And about this - it great working to me without setTextFormat. – Crabar Aug 03 '14 at 08:57
  • I actually found a solution if there is only tags in the string: `feedbackTextField.setTextFormat(boldFormat, (startBoldPos-(i*7)), (closeBoldPos-(i*7)-3));` However, other tags like
    adds to the string, which messes up the index of where to place textFormat... Seems like I'll have to look for other tags, and add these to the character index count of the string before setting the textFormat?
    – Hans Magnus Aug 03 '14 at 11:22
  • I am think that you are going in wrong way. Look at my answer update: I have string with embeded font and with correct formatting. Are you need something more? If you need to parse tags and format text appropriate than you need more hard algoritm. – Crabar Aug 03 '14 at 13:33
  • I think I probably not should use textFormat for this after all... My problem is that I'm using Myriad Pro and Myriad Pro Bold. Both fonts embedded in the library. But when I embed the textfield, only one font is displayed. This is why I thought I had to use textFormat. How do I get the bold font to be displayed when there is -tags in the text? – Hans Magnus Aug 04 '14 at 08:04
  • I'm sorry, I didn't see the update before I commented again. Is the Embed code not Flex syntax? I'd rather not include Flex SDK. The fonts are already embedded in the library, and exported for AS. – Hans Magnus Aug 04 '14 at 10:28
  • Embed metadata tag can be used in pure AS3 project. Your problem then maybe in incorrect embedding. Are you sure that fonts embedded correctly? If you look on my example, you can see that I set fontWeight="bold" for bold font - it's important part. Maybe the problem is connected with this? – Crabar Aug 04 '14 at 11:01
  • Works! Thanks a lot for the suggestion! The swf file increased from 29 KB to 142 KB, so I have embedded all characters I guess. Is there a way to code which characters and glyphs etc to embed, similar to the way done in the Library (the check boxes)? Also, in my AS project, I probably should have a central global access point to the fonts, so I don't have to embed them in every class using them? – Hans Magnus Aug 04 '14 at 12:25
  • I recommend you to read some articles about embedding resources and docs from Adobe. This comments is already too long =) And I think that your question may be closed. – Crabar Aug 04 '14 at 15:20