How do you word wrap a RichTextField for Blackberry
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 wi开发者_运维技巧de. 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?
I don't have an exact solution to your problem but in terms of measuring the width of text, if you have a reference to the Font object for the font being used in your field, you can call Font.getAdvance() to get the width of the specified text in pixels. This might help if you have to insert manual breaks in your text.
精彩评论