How do you word wrap a RichTextField for Blackberry

Posted by Kai on Stack Overflow See other posts from Stack Overflow or by Kai
Published on 2010-04-26T20:41:32Z Indexed on 2010/04/26 20:43 UTC
Read the original article Hit count: 462

Filed under:
|
|
|

I've been trying to modify a rich text field to display correctly in its half of the horizontal field. The goal is this:

---------------------------
| address is | ***********|
| very long  | ** IMAGE **|
| state, zip | ***********|
---------------------------

Where address is a single string separate from the city and zip. I am modifying the address field like this:

RichTextField addrField = new RichTextField(address) {
    public int getPreferredWidth() { 
     return 200; 
    }

    protected void layout(int maxWidth,int maxHeight) {
     super.layout(getPreferredWidth(),maxHeight);
     setExtent(getPreferredWidth(), getHeight());
    }
};

The results look like this:

-----------------------------
| address is ve| ***********|
| state, zip   | ** IMAGE **|
|              | ***********|
-----------------------------

where clearly the address is just going under the image. Both horizontal fields are static 200 pixels wide. It's not like the system wouldn't know where to wrap the address. However, I have heard it is not easy to do this and is not done automatically.

I have had no success finding a direct answer online. I have found people saying you need to do it in a custom layout manager, some refer to the RichTextField API, which is of no use. But nobody actually mentions how to do it.

I understand that I may need to read character by character and set where the line breaks should happen. What I don't know is how exactly to do any of this. You can't just count characters and assume each is worth 5 pixels, and you shouldn't have to. Surely there must be some way to achieve this in a way that makes sense.

Any suggestions?

© Stack Overflow or respective owner

Related posts about blackberry

Related posts about word