Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A
Accepted Solution

setTextFormat() More Than Once on Same String?

i'm unsuccessfully attempting to assign a textFormat to two different parts of the same string, but the second time it doesn't register and remains the default text format. both styles (regular and bold) of the font are embedded.

 

 

//Create Text Field
private function createAboutWindowTextField():TextField
    {
    var aboutWindowFont:Font = new AboutWindowFont();

    var regularFormat:TextFormat = new TextFormat();
    var boldFormat:TextFormat = new TextFormat();

    regularFormat.size = boldFormat.size = 12;
    regularFormat.font = boldFormat.font = aboutWindowFont.fontName;
    regularFormat.align = boldFormat.align = TextFormatAlign.CENTER;
    boldFormat.bold = true;

    var result:TextField = new TextField();
    result.antiAliasType = AntiAliasType.ADVANCED;
    result.autoSize = TextFieldAutoSize.LEFT;
    result.defaultTextFormat = regularFormat;
    result.embedFonts = true;
    result.multiline = true;
    result.selectable = false;
    result.type = TextFieldType.DYNAMIC;

    result.text =   "First Header\n" +
                    "Version 1.0\n" +                                                           
                    "Copyright © 2011\n\n" +

                    "Second Header:\n" +
                    "Other info";                                                       

    result.setTextFormat(boldFormat, result.text.indexOf("First Header"), ("First Header").length);
    result.setTextFormat(boldFormat, result.text.indexOf("Second Header:"), ("Second Header:").length);

    return result;
    }

 

 

the above code should result in both "First Header" and "Second Header:" becoming bold, but only "First Header" will be set as bold while "Second Header:" seems to be simply ignored. what's the problem?

 

i've been told it would be easier to use StyleSheet / HTMLText, but i'd rather not and i'm not sure if they were saying it's not possible to apply multiple instances of the same textFormat to a textField without using StyleSheet and HTMLText, which i would find ridiculous if true.


PlayBook Applications:
Drop Swatch
Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition
My Carrier: Rogers

Re: setTextFormat() More Than Once on Same String?

I would personally just create my Texf formats as Globals, then make 2 different strings, just append the second to the first, or position it directly next to the first. that might just be me though.
CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: setTextFormat() More Than Once on Same String?

[ Edited ]

this string is center justified.  your solution wouldn't be so ideal on, say, long center-justified lists with bold headers.  my posted code is more or less just an example of the problem.

 

i'm astounded that i'm having a problem with such a seemingly simple task.


PlayBook Applications:
Drop Swatch
Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition
My Carrier: Rogers

Re: setTextFormat() More Than Once on Same String?

maybe an html text field might be a simpler idea for now. I haven't had luck with this in the past either, thats why i looked to alternative ways. sorry that i cant help much more than this.
CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: setTextFormat() More Than Once on Same String?

somoene in another thread answered this question.  the 3rd parameter in my setTextFormat is incorrect.  i was specifying a length not an endIndex.  i am officially retarded.


PlayBook Applications:
Drop Swatch
Developer
Posts: 327
Registered: ‎12-13-2010
My Device: Bold 9900/Z10 Special Edition
My Carrier: Rogers

Re: setTextFormat() More Than Once on Same String?

could you post your working code please?
CEO/Lead Developer - Tensioncore™ Design Studios
http://tncr.ws/ - ShortenThis!™ for Blackberry Playbook | ShortenThis!™ for BlackBerry® 10
The Periodical Toe A highly informative Table of Elements
DrawThis! A fun drawing app for the BlackBerry® PlayBook™!
http://www.tensioncore.com/ - Tensioncore™ Web Hosting
Highlighted
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: setTextFormat() More Than Once on Same String?

[ Edited ]

 

package
{
//Imports
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.AntiAliasType;
import flash.text.Font;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
import flash.text.TextFormat;

//SWF Metadata Tag
[SWF(backgroundColor = "#000000")]

//Class
public class Test extends Sprite
    {
    //Embedded Fonts
    [Embed (source = "MyriadPro-Regular.otf", fontName = "myriadProRegular", fontStyle = "normal", fontWeight = "normal", mimeType = "application/x-font", embedAsCFF = "false")]
    public static var MyriadProRegular:Class;
	
    [Embed (source = "MyriadPro-Bold.otf", fontName = "myriadProBold", fontStyle = "normal", fontWeight = "bold", mimeType = "application/x-font", embedAsCFF = "false")]
    public static var MyriadProBold:Class;
	
    //Constructor
    public function Test()
        {       
	stage.scaleMode = StageScaleMode.NO_SCALE;
	stage.align = StageAlign.TOP_LEFT;
	stage.frameRate = 60;
		
	init();
        }
		
    //Initialize
    private function init():void
	{
	var defaultFormat:TextFormat = new TextFormat(new MyriadProRegular().fontName, 25, 0x999999);
	var headerFormat:TextFormat = new TextFormat(new MyriadProBold().fontName, 35, 0xFFFFFF);

	var tf:TextField = new TextField();
	tf.antiAliasType = AntiAliasType.ADVANCED;
	tf.autoSize = TextFieldAutoSize.LEFT;
	tf.defaultTextFormat = defaultFormat;
	tf.embedFonts = true;
	tf.multiline = false;
	tf.selectable = false;
	tf.type = TextFieldType.DYNAMIC;
		
	tf.text = "Header A:\n" +
		  "Paragraph A.\n\n" +
		  "Header B:\n" +
		  "Paragraph B.\n\n" +
		  "Header C:\n" +
		  "Paragraph C.";
		
	for each  (var header:String in ["Header A:", "Header B:", "Header C:"])
		  tf.setTextFormat(headerFormat, tf.text.indexOf(header), tf.text.indexOf(header) + header.length);		
		
	addChild(tf);
	}
    }
}

 

 


PlayBook Applications:
Drop Swatch