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

Native Development

Reply
Developer
Posts: 238
Registered: ‎12-26-2010
My Device: BlackBerry Z10
My Carrier: Telus
Accepted Solution

Apply a TextStyle to a Label

Hi, How do we apply a textStyle to a label in c++?

The code below doesnt work.

 

Any thoughts?

 

	TextStyle *textStyle = new TextStyle();
	textStyle->setFontSizeValue(FontSize::Large);
	textStyle->setFontWeight(FontWeight::Bold);

	Label *appNameLabel=Label::create()
			.text("Bold Text")
			.textStyle(textStyle);// THIS LINE CAUSES ERROR

 

    TextStyle *appNameLabelTextStyle = new TextStyle();
    appNameLabelTextStyle->setFontSizeValue(FontSize::Large);
    appNameLabelTextStyle->setFontWeight(FontWeight::Bold);

    Label *appNameLabel=Label::create()
                                .text(AppConstants::getAppName())
                                .textStyle(appNameLabelTextStyle)
                                .horizontal(HorizontalAlignment::Center)
                                .vertical(VerticalAlignment::Center);

www.jackdevora.com
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Apply a TextStyle to a Label

Change it to this,

 

.textStyle(*textStyle);


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 238
Registered: ‎12-26-2010
My Device: BlackBerry Z10
My Carrier: Telus

Re: Apply a TextStyle to a Label

Works like a charm!
www.jackdevora.com
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Apply a TextStyle to a Label

[ Edited ]

Hi,

 

TextStyle *textStyle = new TextStyle();

 

This will leak memory unless deleted manually. TextStyle doesn't have a parent and will not be destroyed by it's parent. Also, the app will not be terminated properly if there are unreleased TextStyle-s.

 

Create it on the stack instead:

 

TextStyle textStyle;
textStyle.setFontSizeValue(FontSize::Large);
textStyle.setFontWeight(FontWeight::Bold);

Label *appNameLabel = Label::create()
	.text("Bold Text")
	.textStyle(textStyle);

 

upd. parent/child relationship is not working in this case because TextStyle is a simple structure which doesn't inherit from QObject.  .textStyle() setter just copies it's contents and doesn't take the ownership of original structure, so you'll have to destroy it manually, or create it on stack so it's destroyed at the end of { } block.

 


Andrey Fidrya, @zmeyc on twitter
Highlighted
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Apply a TextStyle to a Label

[ Edited ]

Or simply destroy it in the class destructor as I do. Smiley Happy

 

Either's good, Zmey's way means that you don't have to remember to delete it however.

 

Whichever you are most comfortable with as a programmer.

 

 

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.