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

Java Development

Reply
New Developer
Posts: 160
Registered: ‎03-02-2011
My Device: Not Specified

Change font of Edittext Field

Hello,

 

I want to make an editField with the following behaviour, I want the user to type in it his text and by code i want to change the font of some characters in it not all of it. How can I do that? Is there any field other than editfield who can do this?

 

Anyone can help please?

Developer
Posts: 202
Registered: ‎04-19-2011
My Device: Blackberry Bold 9780
My Carrier: TAM

Re: Change font of Edittext Field

you can use void paint for access the font using setFont and using drawText

but it still need a lot of effort to create multiple font type in a field...

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: Change font of Edittext Field

[ Edited ]

To expand on what welsenKiriono said,

 

class EditFieldTwoFonts extends EditField{
    EditFieldTwoFonts(String label, String initialValue) {    
        super(label, initialValue);
    }
    protected void paint(Graphics graphics) {
        boolean even=true;
        graphics.drawRect(0,0,getWidth(),getHeight());
        for (int i=0; i<getText().length(); i++) {
            if (even) {
                graphics.setFont(Font.getDefault().derive(Font.BOLD));
                even=false;
            } else {
                graphics.setFont(Font.getDefault().derive(Font.PLAIN));
                even = true;
            }
            graphics.drawText(new Character(getText().charAt(i)).toString(),(i*13),2);
        }
        
        
        
    }
    
} 

 

This code alternates the characters of the input between bold and plain, but you can change the fonts to whatever you want and make your own algorithm for deciding when the fonts change. 

The code is quick and dirty but was the best I could write in the time I had. It needs some fixing in a few areas: actually show the label that's specified by "String label", put the proper spacing between characters, override other constructors (if necessary).

 

Good luck!

 

Scott

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Change font of Edittext Field

The issue with the solutions given is that they will be difficult to implement in an input Field - because you will need to manage cursor positioning and text selection amongst other things.  If you were just going to have this as an output Field, then you would probably use a RichTextField, see:

http://supportforums.blackberry.com/t5/Java-Development/Format-text-in-a-RichTextField/ta-p/445038

 

I don't think there is an input Field that matches what you want.  And I can not see how this sort of input Field could be developed in a reasonable time frame, I think you are talking days of work.  If you really really need this I would consider having an unformatted input area and a formatted 'display' area, with the formatting being done using RichTextField.  But that is a little clunky.....

New Developer
Posts: 160
Registered: ‎03-02-2011
My Device: Not Specified

Re: Change font of Edittext Field

Hey all,

I chose to do a custom edit field where I will place the super.paint(graphics); and when i find this character i will add 2 spaces after it and redraw the character with bigger font in its position. It is working great but the problem is that each time I input a character or move the selection the paint method is re-executing about 30 times.. I don't know what is the reason behind that

Does anyone have a clue about that problem?
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Change font of Edittext Field

No idea sorry and I think that you will spend a great deal of time playing round with issues like this, hence my previous comment.  Also what is going to happen when the user scrolls back to delete your character?  And how do you find the position of the character if it is on the second line?