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
Trusted Contributor
itcraps
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
Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
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. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Trusted Contributor
itcraps
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
Please use plain text.
Developer
Zmey
Posts: 1,509
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

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
Please use plain text.
Developer
BBSJdev
Posts: 5,948
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Apply a TextStyle to a Label

[ Edited ]

Or simply destroy it in the class destructor as I do. :smileyhappy:

 

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. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.