Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11991 Posts in 1587 Topics- by 3508 Members - Latest Member: PienueDut

26. May 2012, 10:44:10 am
Xith3D CommunityXith3D InternalsDeveloper discussion (Moderators: Marvin Fröhlich, 'n ddrylliog)LineWrapping TextList
Pages: [1]
Print
Author Topic: LineWrapping TextList  (Read 811 times)
Gunslinger
Enjoying the stay
*
Offline Offline

Posts: 44


View Profile
« on: 12. March 2009, 07:44:24 am »

I have extended the TextList widget with linewrapping methods. What do you think?

It still remains to break words that don't fit on one line by themselves.

Code:
    public java.util.List<Label> addItemLineWrap(String text)
    {
        return this.addItemLineWrap(text, null, null);
    }

    public java.util.List<Label> addItemLineWrap(String text, Font font)
    {
        return this.addItemLineWrap(text, null, font);
    }

    public java.util.List<Label> addItemLineWrap(String text, Colorf color)
    {
        return this.addItemLineWrap(text, color, null);
    }

    public java.util.List<Label> addItemLineWrap(String text, Colorf color, Font font)
    {
        java.util.List<Label> labels = new ArrayList<Label>();
        Scanner scanner = new Scanner(text);
        StringBuffer line = new StringBuffer();
        Label label = addItem("", font, color);
        Dim2f dim = new Dim2f();
        while (scanner.hasNext())
        {
            String word = scanner.next();
            if (scanner.hasNext())
            {
                word = word + " ";
            }
            label.setText(line.toString() + word);
            label.getMinimalSize(dim);
            if (dim.getWidth() > this.getWidth() - 20)
            {
                label.setText(line.toString());
                line.delete(0, line.length());
                label = addItem(word, font, color);
            }
            line.append(word);
        }
        return labels;
    }

Logged
Gunslinger
Enjoying the stay
*
Offline Offline

Posts: 44


View Profile
« Reply #1 on: 12. March 2009, 06:33:56 pm »

and of course, -20 should be replaced by left and right padding (and then some)...
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #2 on: 12. March 2009, 08:51:48 pm »

I am definitely happy about this contribution. But it might be a little confusing as a new set of methods for the TextList. I would have expected, that this method creates one new item with wrapped text. Would you agree, that your code was added in a utility class for text lists? Maybe you want to have dev access to the xith SVN. Then you could even take credit for it in the SVN log Smiley.

Marvin
Logged
Gunslinger
Enjoying the stay
*
Offline Offline

Posts: 44


View Profile
« Reply #3 on: 14. March 2009, 11:18:02 am »

In my own project I extend TextList to LineWrappedTextList. It is a simple solution that works for me.

A multi-line label that does linewrapping, breaks words that are to long and handles newlines would be great. Will think about it and look into how labels work.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic