BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 08-20-2007, 09:58 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: TDC
Posts: 115
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default styles and colors on my label, button etc

Please Login to Remove!

is there any way i can change the color/font size etc on the labels that i use??
Offline  
Old 08-20-2007, 11:00 AM   #2 (permalink)
CrackBerry Addict
 
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I usually create my own field class to do this kind of thing:

Code:
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;

public class AlignedLabelField extends Field implements DrawStyle{
    private Font headerFont;
    
    private String title;
    private String content;
    private int anchor;
    
    private int fieldWidth = Graphics.getScreenWidth();
    private int fieldHeight = 24;
    private int titleColour = 0x888888;
    private int contentColour = 0x666666;
    private int backgroundColour = 0;
    
    public AlignedLabelField(String _title, String _content, int _anchor) {
        super(Field.NON_FOCUSABLE);
        headerFont = FieldFont();
        title = _title;
        content = _content;
        anchor = _anchor;
    }
    
    public AlignedLabelField(String _title, String _content, int _anchor, int _backgroundColour) {
        super(Field.NON_FOCUSABLE);
        headerFont = FieldFont();
        title = _title;
        content = _content;
        anchor = _anchor;
        backgroundColour = _backgroundColour;
    }
    
    public void setTitle(String _title){
        title = _title;
        invalidate();
    }
    
    public void setContent(String _content){
        content = _content;
    }
    
    public void setContentColour(int _contentColour){
        contentColour = _contentColour;
        invalidate();
    }
    
    public void setBackgroundColour(int _backgroundColour){
        backgroundColour = _backgroundColour;
        invalidate();
    }
    
    protected void layout(int width, int height) {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }
    
    public int getPreferredWidth() {
        return fieldWidth;
    }
    
    public int getPreferredHeight() {
        return fieldHeight;
    }
    
    protected void paint(Graphics graphics) {
        graphics.setFont(headerFont);
        
        if(backgroundColour != 0){
            graphics.setColor(backgroundColour);
            graphics.fillRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
        }
        
        int yAnchor = (this.getPreferredHeight()-headerFont.getHeight())/2;
        graphics.setColor(titleColour);
        graphics.drawText(title, 1, yAnchor);
        graphics.setColor(contentColour);
        graphics.drawText(content, anchor, yAnchor);
    }
    
    public static Font FieldFont(){
        try {
            FontFamily theFam = FontFamily.forName("SYSTEM");
            return theFam.getFont(net.rim.device.api.ui.Font.PLAIN, 14);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
__________________
new job doesn't allow a public profile - please do not contact this user with questions, you will not get a response. good luck!
Offline  
Old 08-20-2007, 11:08 PM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: Jun 2007
Location: Bangladesh
Model: 8800
PIN: N/A
Carrier: EDGE
Posts: 93
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi genvej,

Quote:
Originally Posted by genvej View Post
is there any way i can change the color/font size etc on the labels that i use??
You can also do it by simply overriding the paint method.

Sample code:

Code:
LabelField customField = new LabelField("Hello World!") {
  // Override the paint method to set color/font size
  public void paint(Graphics graphics) {
    graphics.setColor(Color.RED);
    graphics.setFont(Font.getDefault().derive(Font.PLAIN, 14));
    super.paint(graphics);
  }
};
add(customField);
Enjoy,
ARIF

Last edited by arifzaman : 08-20-2007 at 11:15 PM.
Offline  
Old 08-22-2007, 06:44 AM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Jul 2007
Model: 8800
PIN: N/A
Carrier: TDC
Posts: 115
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by jfisher View Post
I usually create my own field class to do this kind of thing:

Code:
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;

public class AlignedLabelField extends Field implements DrawStyle{
    private Font headerFont;
    
    private String title;
    private String content;
    private int anchor;
    
    private int fieldWidth = Graphics.getScreenWidth();
    private int fieldHeight = 24;
    private int titleColour = 0x888888;
    private int contentColour = 0x666666;
    private int backgroundColour = 0;
    
    public AlignedLabelField(String _title, String _content, int _anchor) {
        super(Field.NON_FOCUSABLE);
        headerFont = FieldFont();
        title = _title;
        content = _content;
        anchor = _anchor;
    }
    
    public AlignedLabelField(String _title, String _content, int _anchor, int _backgroundColour) {
        super(Field.NON_FOCUSABLE);
        headerFont = FieldFont();
        title = _title;
        content = _content;
        anchor = _anchor;
        backgroundColour = _backgroundColour;
    }
    
    public void setTitle(String _title){
        title = _title;
        invalidate();
    }
    
    public void setContent(String _content){
        content = _content;
    }
    
    public void setContentColour(int _contentColour){
        contentColour = _contentColour;
        invalidate();
    }
    
    public void setBackgroundColour(int _backgroundColour){
        backgroundColour = _backgroundColour;
        invalidate();
    }
    
    protected void layout(int width, int height) {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }
    
    public int getPreferredWidth() {
        return fieldWidth;
    }
    
    public int getPreferredHeight() {
        return fieldHeight;
    }
    
    protected void paint(Graphics graphics) {
        graphics.setFont(headerFont);
        
        if(backgroundColour != 0){
            graphics.setColor(backgroundColour);
            graphics.fillRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
        }
        
        int yAnchor = (this.getPreferredHeight()-headerFont.getHeight())/2;
        graphics.setColor(titleColour);
        graphics.drawText(title, 1, yAnchor);
        graphics.setColor(contentColour);
        graphics.drawText(content, anchor, yAnchor);
    }
    
    public static Font FieldFont(){
        try {
            FontFamily theFam = FontFamily.forName("SYSTEM");
            return theFam.getFont(net.rim.device.api.ui.Font.PLAIN, 14);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

thx a bunch.... can you plz plz add me the functionallity of wordwrapping the text... it just continues beyond my screen when i type a lot of content. Im sure its just an attribute to set, however i havent been able to find it
Offline  
Old 08-22-2007, 09:48 AM   #5 (permalink)
CrackBerry Addict
 
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

wrapping would involve some hefty logic i think, but you can use an ellipsis to tidy it up, just change the line that reads:

graphics.drawText(content, anchor, yAnchor);

to:

graphics.drawText(content, anchor, yAnchor, DrawStyle.ELLIPSIS, fieldWidth-anchor);
__________________
new job doesn't allow a public profile - please do not contact this user with questions, you will not get a response. good luck!
Offline  
Old 08-22-2007, 01:08 PM   #6 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I wrote this to do what I think you're wanting. It's not overly complicated

Code:
maxCharsForScreen = baseString.length();
			Vector lineVector = new Vector();
			String[] lines = FWUtils.split(fieldText, "\n");
			for (int i = 0; i < lines.length; i++) {
				if (lines[i].length() > maxCharsForScreen) {
					String line = lines[i];

					while (line.length() > maxCharsForScreen) {

						int index = line.lastIndexOf(' ', maxCharsForScreen);
						if (index > 1) {
							lineVector.addElement(line.substring(0, index));
							line = line.substring(index, line.length());
						} else {

							lineVector.addElement(line.substring(0,
									maxCharsForScreen));
							line = line.substring(maxCharsForScreen, line
									.length());
						}

					}
					line = line.trim();
					lineVector.addElement(line.substring(0, line.length()));

				} else {
					lineVector.addElement(lines[i]);

				}

			}
			textLines = new String[lineVector.size()];
			lineVector.copyInto(textLines);
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.