How to set a font to LabelField text in Blackberry? - blackberry

I don't know how to apply font style to a text in a LabelField in Blackberry.

You can just use LabelField.setFont. If you don't do this explicitly on the label field, the field will use whatever font is used by its manager (and so on upward up the hierarchy).
There are a couple of ways to get a font. One is to derive one from an existing font (in this case I'm getting a bold version of the default font):
LabelField labelField = new LabelField("Hello World");
Font myFont = Font.getDefault().derive(Font.BOLD, 9, Ui.UNITS_pt);
labelField.setFont(myFont);
The other is to get a specific font family and derive a font from that (here getting a 12 pt italic font):
LabelField labelField = new LabelField("Hello World");
FontFamily fontFamily = FontFamily.forName("BBCasual");
Font myFont = fontFamily.derive(Font.ITALIC, 12, Ui.UNITS_pt);
labelField.setFont(myFont);
A couple of things to note: I used UNITS_pt (points) instead of UNITS_px (pixels). This is a good idea generally since BlackBerry devices vary quite a bit in screen size and resolution (DPI) and using points will give you a more consistent look across devices, instead of having your text look tiny on a Bold or 8900 (or huge on a Curve or Pearl).
Also in the second example, forName can throw a ClassCastException which you have to catch (it's a checked exception) but is never actually thrown according to the Javadocs, if you specify an unknown name, it'll fall back to another font family.

Here's a post which has a ResponseLabelField that extends LabelField and shows how to set the font:
http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&thread.id=37988
Here's a quick code snippet for you:
LabelField displayLabel = new LabelField("Test", LabelField.FOCUSABLE)
{
protected void paintBackground(net.rim.device.api.ui.Graphics g)
{
g.clear();
g.getColor();
g.setColor(Color.CYAN);
g.fillRect(0, 0, Display.getWidth(), Display.getHeight());
g.setColor(Color.BLUE);
}
};
FontFamily fontFamily[] = FontFamily.getFontFamilies();
Font font = fontFamily[1].getFont(FontFamily.CBTF_FONT, 8);
displayLabel.setFont(font);
Someone correct me if I'm wrong, but I believe different fonts are chosen by using a different index into the fontFamily array.
EDIT: And here's a test app you can use to switch between fonts: http://blackberry-digger.blogspot.com/2009/04/how-to-change-fonts-in-blackberry.html

Related

How to set bold label with ObjectSetText in MQL

Is there a way to set bold text using ObjectSetText() function in MQL4.
Should a font name be for example "Arial Bold" or can I set a path to the font .ttf-file?
If a path option is possible, is that path relative or absolute?
ObjectSetText() uses O/S-registered fonts & only limited controls
as one may test on GUI panels, MQL4 operations do not have full type-setting font-manipulation controls available via code
( this is all about trading, isn't it? )
Check what fonts are available from your O/S:
( or from used Docker/WINE thin-wrapper container )
So in MQL4 code there will thus simply be a string-typed or #define-ed literal specification of the font name and one may additionally set aFontSIZE + aFontCOLOUR attribute(s)
#define clrSignalLABEL clrAqua // LITERAL-way
#define iLabelFontSIZE 24
string signalTextFONT 'Times New Roman'; // STRING-way
input bool Font_Bold = true;
string FB;
int init()
{
if(Font_Bold == true)
{
FB = "Arial Bold";
}
else
{
FB = "Arial";
}
}
ObjectSetText("name", Text, FontSize, FB, FontColor);

Unable to change width of TextField in Blackberry

I am a newbie to Blackberry, I was just trying out some sample apps in Blackberry. I tried to create a login page. In that, when I tried to change the width of the text field, the text field became invisible.
The below is part of the code to create the TextField.
super(Field.FIELD_VCENTER);
......
t_username = new TextField()
{
public void layout(int width, int height)
{
super.layout(500, 30);
setExtent(500, 30);
}
};
t_username.setMaxSize(10);
t_username.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1),Border.STYLE_SOLID));
I tried to create a border to check where it is coming or not, but couldn't find it.
PFB, the snapshot:
Overriding the TextField#layout() method
public void layout(int width, int height)
{
super.layout(500, 30);
setExtent(500, 30);
}
is one way that you can set a text field's width. So, I think there's something else going wrong here.
1) Possibly, when you changed your code, your mistakenly removed the call to
add(t_username);
you don't show us where you actually add that field, so if you're not calling add(t_username) somewhere else, it's not going to be visible. Fields must be added to a Manager or Screen to be visible.
2) Perhaps some other code you've written (but not shown) is attempting to do something with a Graphics object. For example, if you're overriding a paint(Graphics) method in another field, you may be changing a color (e.g. graphics.setColor(Color.WHITE)) and not remembering to reset the original color. Possibly, your text field is there, but it's the same color as its background. If this was happening, though, you could still see the text field cursor when you give that field focus. I simply can't tell from your screenshot.
A Better Way
Normally (but not always), it should be the responsibility of the Manager/Screen that contains the field to determine its size. I think it's poor encapsulation to have most fields set their own width (although there are exceptions to this). I would recommend using setMargin() and the USE_ALL_WIDTH flag to set a reasonable width for this text field:
public class TextFieldScreen extends MainScreen {
private TextField t_username;
public TextFieldScreen() {
super(Field.FIELD_VCENTER);
HorizontalFieldManager row = new HorizontalFieldManager();
LabelField label = new LabelField("Username");
label.setMargin(new XYEdges(2, 0, 2, 10));
row.add(label);
t_username = new TextField(TextField.USE_ALL_WIDTH);
t_username.setMaxSize(10);
t_username.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1), Border.STYLE_SOLID));
t_username.setMargin(new XYEdges(2, 10, 2, 10));
row.add(t_username);
add(row);
}
}
One benefit of this solution, compared to hard-coding a width of 500, is that if your app supports portrait/landscape rotation, the code above will correctly adjust the field width as the screen width changes. If you hard-code the field width, the field will be too wide when the device rotates to portrait.

How to customize the button and label text size for each field separately in blackberry application?

I am new to BB application. I want customized the size for the button text and label text.
I am using this code
try {
FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif");
Font appFont = alphaSansFamily.getFont(Font.PLAIN, 9, Ui.UNITS_pt);
setFont(appFont);
} catch (ClassNotFoundException e) {
}
but using this it will change size for what all the field we are using within the constructor.But I need different size for different field .Like Title will be big size and other label text button text will be small size.
I am guessing that you are calling that when you are creating the screen so the setFont() changes the font for the full screen.
You can call set font on the required button or label and it will change the font only for that field
e.g button.setFont(yourFont)
Are you calling setFont() explicitly on the Field you wish to change?
just in the above try block donot declere the statement setFont(appFont), this sets the font of the whole screen.
Insead set the font by specifying which label or button you want to set the font like
LabelField lf1 = new LabelField("Testing");
lf1.setFont(appFont);
Again
ObjectChoiceField ocf1 = new ObjectChoiceField("","","");
ocf1.setFont(appFont);

How to use Different font for LabelField like Arial,TimesNewRoman in BlackBerry

Hi friends I am using LabelFields to display text message in a screen i want different fonts each LabelField but when i am using api fonts of blackberry using
FontFamily fontFamily[] = FontFamily.getFontFamilies();
Font font2 = fontFamily[0].getFont(FontFamily.SFF4_FONT,18);
is showing bold style how to use different font styles plz help me
try this
FontFamily alphaSerifFamily = FontFamily.forName("BBAlpha Serif");
Font appFont = alphaSerifFamily.getFont(Font.PLAIN,7,Ui.UNITS_pt);
LabelField lb = new LabelField("Test");
lb.setFont(appFont);

Blackberry: Bold Font for LabelField text

I am having the following code snippet in my application screen to show a label text in some bigger and good font. It looks good to me. But i also want to make this font to "bold" as well. Currently it doesn't show in boldfont as well. Could someone guide me how can i provide the label field text with the same font but also with bold ??? (or) Please tell me how can i make the label text with any other bigger size(24) font with bold?
LabelField subTitleLabel = new LabelField ("My App Sub Title", LabelField.FIELD_LEFT);
final FontFamily fontFamily[] = FontFamily.getFontFamilies();
final Font font10 = fontFamily[1].getFont(FontFamily.CBTF_FONT, 24);
subTitleLabel.setFont(font10);
subTitleLabel.setMargin(25, 0, 0, 10);
horEventFldManager.add(subTitleLabel);
This assumes that subTitleLabel is already set to the font that you want. It is just resetting it to a Bold version of that font.
subTitleLabel.setFont(subTitleLabel.getFont().derive(Font.BOLD));

Resources