Cannot use custom font in Cocos2d-x project - c++

This code works:
auto label = LabelTTF::create(CokeStore::name, "Marker Felt", 170);
But none of these work:
auto label = LabelTTF::create(CokeStore::name, "Coca Cola ii", 170);
auto label = LabelTTF::create(CokeStore::name, "CocaColaii", 170);
auto label = LabelTTF::create(CokeStore::name, "cocacolaii", 170);
I imported all the fonts as resources into my project and added fonts' path into Fonts provided by application of Info.plist. I'm using Cocos2d-x v. 3, the latest version from Github.
Anybody can help me with this? Is there anything wrong with my code and how to fix them?
Thanks for your help!

I was facing alot of difficulty adding a custom font. I know the question has already been answered but I just want to share what solved the problem for me.
These are the points you need to ensure to use a custom font:
Font name must match the PostScript name of that font (you can easily find the postscript name using the Font Book, just select the font and press command+i) , so your font name should be: PostScriptName.ttf
In the Info.plist of your project, make sure to add the font name under "Fonts provided by application", just put in the font name and extension for item0 or whatever the length of your font stack is: PostScriptName.ttf,
Now, the next step that solved the issue for me was:
Ensure that the font file exists in the "Copy Bundle Resources" Section of the 'Build Phases' settings for your project. For me it somehow wasnt there and thats why the font wasnt loading, so I just manually added it.
Now you can use it even with LabelTTF::create("Text", "PostScriptName", 24)!

In Cocos2d-x V3, there is a new class Label.
I tried this and it worked:
Label *label = Label::createWithTTF("Your string here","cocacolaii.ttf", 170);
For more details, you can read this

Related

How to load multiple font of same familiy

I am writing an application using the Qt framework. In the display, I have to show multiple information, but using different types of font of the same family, Montserrat.
What I have done so far to load the fonts is:
int ultralightid = QFontDatabase::addApplicationFont(":/Montserrat_UltraLight.tff");
QString UltraFont= QFontDatabase::applicationFontFamilies(ultralightid ).at(0);
QFont font1(UltraFont,QFont::Normal);
font1.setPixelSize(50);
int lightid = QFontDatabase::addApplicationFont(":/Montserrat_Light.tff");
QString LightFont= QFontDatabase::applicationFontFamilies(lightid).at(0);
QFont font2(LightFont,QFont::Normal);
font2.setPixelSize(150);
label1->setFont(font1);
label2->setFont(font2);
label1->setText("bla bla");
label2->setText("bla bla");
The font sizes are correct, but the font itself it is not. From what I have noticed (trying with Hairline_Montserrat,Light_Montserrat,UltraLight_Montserrat), it is as if the fonts have a sort of priority. If I declare them all, all the fonts are the Light one, if I comment that font type, all of them are Hairline one, otherwise (last priority) the labels use the ultralight font.
I have tried adding other font type (from other families) and in that case my code works correctly.
If I use
qDebug()<<QFontDatabase::applicationFontFamilies(ultralightid);
qDebug()<<QFontDatabase::applicationFontFamilies(lightid);
both of them print the family "Montserrat".
I use the qrc file and the AUTORCC flag in the CMAKE (it should be similar using qmake) and all the file are uploaded correctly.
Do you know if there is another way to add fonts of the same family? Or is there something I am doing wrong?
Here are the fonts:
https://www.onlinewebfonts.com/download/9d31c906a6cc6064bbe7d33d51058317 light
https://it.allfont.net/download/montserrat-light/ ultralight
This is an old question but I was just struggling with exactly the same problem when trying to load normal, bold, ... versions of a font family in Qt.
I solved the problem (although in a somewhat hacky way) by simply giving each of the ttf files a different family name. I used Typograf, simply open the font, right click to open properties and then click rename. There are probably many other tools that do this too.
You don't need to manage font files from one family separatelly.
I suggest this solution:
Create a folder with all ttf's of the same family.
Load all files from the folder via id = QFontDatabase.addApplicationFont(path)
Collect all font families from these files via QFontDatabase.applicationFontFamilies(id)
Check if only one and desired family is loaded, and the family name is exactly the same as requested, or warn the user about these errors.
Create font object font = QFont(family)
Then for example, font.setItalic(True). If Italic version of family is loaded, it will be used, otherwise it will be created from Regular by QT.

Qt Image won't show up in button

I am creating a Qt application and I have an image that I want to use for a button instead of text. Unfortunately all that shows up is an empty button.
I've tried two different methods to get it to show up with the same results for both methods.
Code for Method 1:
ui->setupUi(this);
QPixmap pix(":/svg/resources/menu.svg");
int w = ui->menuButton->width();
int h = ui->menuButton->height();
ui->menuButton->setMask(pix.scaled(w,h,Qt::KeepAspectRatio).mask());
I found the info for the second method here: Adding image to QPushButton on Qt
Code for Method 2:
ui->setupUi(this);
QIcon icon(":/svg/resources/menu.svg");
ui->menuButton->setIcon(icon);
Could someone please help me figure out why my image isn't showing up and the button is just empty?
In my project using .svg images as button icons are no problem, maybe the button size is missing, try:
ui->menuButton->setIcon(QIcon(":/svg/resources/menu.svg"));
ui->menuButton->setToolTip("optional tooltip");
ui->menuButton->setFixedSize(QSize(28,28));
Assuming you stored your icons correctly in a resource file. If not, create a new:
right click on your top project folder (in the project-tree) -> Add new.. -> choose Qt on the left an Qt Resource File on the right window -> a new Window apears.
Add Prefix -> Add Files (your icon)
You use .svg image format. Are you sure your application load image format plugin for .svg? Image plugins must be in directory "imageformats" in current directory of your application. Avaliable plugins you can find in Qt directory .../Desktop/Qt/<version>/<mingw or msvc>/plugins/imageformats

QT QIcon properties for custom widget in designer

I have been working for a little while now on creating a QT custom designer widget for GUI menus. The idea being that you simply drag it into the designer, select the number of frames you'd like, how many buttons per frame, etc. and it generates and sizes everything for you.
The way the widget is structured there are properties to configure each button for the frame you are in. For example, you would use the button0Text field to enter text under Button0 while editing in frame 0, then use it again to edit Button0 which is in frame 1. Both buttons would retain the individual changes for each frame.
The Problem
Normally when I switch frames all of my properties are updated to reflect the status of the frame. The exception being QIcon. The correct icon is retained in the actual graphical representation and builds correctly, however the file path in the property list is always of the last edited for that property. I think this will be extremely confusing to an end user and I have found no way to fix it. So for example, if I set text and icons in frame 0 then switch to frame 1 the text in the property list will update to reflect the state of frame 1 but the icon path names will still show my last edit in frame 0 and not the actual icon in frame 1.
I have tried things as simple as:
setProperty("button0Icon", getButton0Icon());
That code works on properties like text, but not for the icon. I try executing it immediately after changing frames.
I have also tried:
#ifndef Q_WS_QWS
QDesignerFormWindowInterface *form = QDesignerFormWindowInterface::findFormWindow(this);
if(form){
QDesignerFormEditorInterface *editor = form->core();
QExtensionManager *manager = editor->extensionManager();
QDesignerPropertySheetExtension *sheet;
sheet = qt_extension<QDesignerPropertySheetExtension*>(manager, this);
int propertyIndex = sheet->indexOf("button0Icon");
sheet->setChanged(propertyIndex, true);
sheet->setProperty(propertyIndex, getButton0Icon());
}
#endif
And:
int propertyIndex = this->metaObject()->indexOfProperty("button0Icon");
QMetaProperty property = this->metaObject()->property(propertyIndex);
property.write(this, QIcon());
Nothing seems to update the property list in the designer.
I have all properties, including all QIcon properties properly declared in the header file with Q_PROPERTY and assigned getter and setter functions.
To be clear, the icon values are indeed retained through each frame when compiled. So it is functioning, just unclear for most users.
If anyone has any experience with this or any ideas please let me know. Thanks.
I have discovered that QIcon does not store file names/paths. The file names are only used for the creation of the QIcon. I think this is most likely the reason I do not get any feedback in the Property Browser for my QIcon properties.
Instead I have chosen to hide this property in the designer and add three new ones. Three QUrl properties, each of which is used to supply an image file. I use three because I want to construct a QIcon that contains Modes/States for normal, disabled, and pressed operations.
I take each of these QUrls and save them in QStringLists behind the scenes so their values are stored. I then construct my QIcon using the file names provided from the QUrls.
I would much prefer to be using the native QIcon in the designer for this, any thoughts or feedback are appreciated.

How can i add custom fonts in cocos2d-x

To add custom fonts in cocos2d-x i followed the following steps.
Download a font.
paste it in resource file of my project without any subfolder.
Add my font in info.plist in Fonts provided by application as Roboto-thin.ttf
Add it in Copy bundle resources.
I used the following code to display the font:
CCLabelTTF* cycleLabel1 = CCLabelTTF::create(" ", "Roboto-Thin.ttf", 80);
I tried with removing the extension also in the code, but no use. Anyone please help me to solve this problem.
On IOS, inside your CCLabelTTF::create() you have to use the NAME of the font, not the file of the font. In your case it is probably "Roboto Thin". Something like this:
CCLabelTTF* cycleLabel1 = CCLabelTTF::create(" ", "Roboto Thin", 80);
You can see font names installing them and opening them in Font Book (assuming you use mac)
Note on Android you still call fonts using its file name.
It is very explained (not very well at all) in the documentation.
http://www.cocos2d-x.org/wiki/How_to_Use_Custom_TTF_Font_on_iOS
Try this:
AddFontResource(L"xxx/yyy/Roboto-Thin.ttf");
before CCLabelTTF::create(" ", "Roboto-Thin", 80);
and then RemoveFontResource(L"xxx/yyy/Roboto-Thin.ttf");
Make sure that the name of the font and the file name are the same

Cocos2d-Android Custom font not working

I am trying to use custom font in Cocos2d-Android with CClabel, I have added my font file into the assets and trying to use it by name "Faraco_Hand.ttf" and "Faraco_Hand" but its not working, I have googled around but haven't found any solution. this is how my code looks a like:
currentVideoLabel = CCLabel.makeLabel(VideosLabels[currentSelected], "Faraco_Hand.ttf", winSize.width/41);
currentVideoLabel.setPosition(CGPoint.make(winSize.width/2, 20));
addChild(currentVideoLabel);
If someone know this please help me.
Hi i use this way to add font in my game here i am sharing you my code check it hope it's helpful for you here i am setting my font on label this is the only way to set fonts...all the best
Example:1
CCLabel labelcontinue = CCLabel.makeLabel("continue", "fonts/JimboStd-Black.otf", 14);
labelcontinue.setPosition(CGPoint.ccp(272, 95));
labelcontinue.setColor(ccColor3B.ccc3(165, 42, 42));
mainPaperNode.addChild(labelcontinue, 25);
Example2:
CCLabel labelWelcome = CCLabel.makeLabel("Welcome", "fonts/JimboStdBlack.otf",20);
labelWelcome.setColor(ccColor3B.ccc3(139, 69, 19));
labelWelcome.setPosition(CGPoint.ccp(innerArea.getContentSize().getWidth() / 2, (innerArea.getContentSize().getHeight() / 2) + 138));
innerArea.addChild(labelWelcome);
i know this is late reply, anyhow it may help someone who search this same thing...
CCLabel supports external fonts but some of the ttf will not recognized by android so it will through exception so better you try another font instead of this the code is same...
label = CCLabel.makeLabel("label value", "external_font_name.ttf", 30);
label.setPosition(CGPoint.make(winSize.width/2, 20));
addChild(label);
I don't think there's any problem with your method. I guess the problem is you are renaming the original font file. Rename the font file name with its original name (written in the font file itself, check attached image).
in this case, I kept my ttf file in Resources/fonts folder and wrote my code like:
CCLabelTTF* label = CCLabelTTF::create("Hello World", "fonts/A Damn Mess.ttf", 20);
Also check CCLabelTTF instead of CCLabel.
Hope this will help you.
Hai this will help. This code works for me fine. Put custom font int assets->fonts
CCLabel label1 = CCLabel.makeLabel("Points: 0", "fonts/pin.ttf", 28);
label1.setColor(ccColor3B.ccWHITE);
label1.setPosition(280f * scaleX, 550f * scaleY);
addChild(label1);