如果太长</string>,则将字符串拆分为List <string>

时间:2012-01-25 16:00:56

标签: java string graphics awt

我有这个字符串:“这是我非常长的字符串,不适合一行” 我需要将它分成多行,以便它适合我需要的地方。

让我们说每行只有大约15个字母的空间,那么它应该是这样的:

"This is my very" 
"long String" 
"that wont fit"
"on one line"

我想把它分成List<String>所以我可以做

for(String s : lines) draw(s,x,y);

任何关于如何做到这一点的帮助都会受到关注!

我正在渲染文本的方式是使用Graphics.drawString()

这是我到目前为止所尝试过的(可怕的,我知道)

String diaText = "This is my very long String that wont fit on one line";
String[] txt = diaText.trim().split(" ");
int max = 23;
List<String> lines = new ArrayList<String>();
String s1 = "";
if (!(diaText.length() > max)) {
    lines.add(diaText);
} else {
    for (int i = 0; i < txt.length; i++) {
        String ns = s1 += txt[i] + " ";
        if (ns.length() < 23) {
            lines.add(s1);
            s1 = "";
        } else {
            s1 += txt[i] + "";
        }
    }
}
int yo = 0;
for (String s : lines) {
    Font.draw(s, screen, 70, 15 + yo, Color.get(-1, 555, 555,555));
    yo += 10;
}

2 个答案:

答案 0 :(得分:1)

Label rendered on image

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class LabelRenderTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {

            String title = "<html><body style='width: 200px; padding: 5px;'>"
                + "<h1>Do U C Me?</h1>"
                + "Here is a long string that will wrap.  "
                + "The effect we want is a multi-line label.";

                JFrame f = new JFrame("Label Render Test");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                BufferedImage image = new BufferedImage(
                    400,
                    300,
                    BufferedImage.TYPE_INT_RGB);
                Graphics2D imageGraphics = image.createGraphics();
                GradientPaint gp = new GradientPaint(
                    20f,
                    20f,
                    Color.red,
                    380f,
                    280f,
                    Color.orange);
                imageGraphics.setPaint(gp);
                imageGraphics.fillRect(0, 0, 400, 300);

                JLabel textLabel = new JLabel(title);
                textLabel.setSize(textLabel.getPreferredSize());

                Dimension d = textLabel.getPreferredSize();
                BufferedImage bi = new BufferedImage(
                    d.width,
                    d.height,
                    BufferedImage.TYPE_INT_ARGB);
                Graphics g = bi.createGraphics();
                g.setColor(new Color(255, 255, 255, 128));
                g.fillRoundRect(
                    0,
                    0,
                    bi.getWidth(f),
                    bi.getHeight(f),
                    15,
                    10);
                g.setColor(Color.black);
                textLabel.paint(g);
                Graphics g2 = image.getGraphics();
                g2.drawImage(bi, 20, 20, f);

                ImageIcon ii = new ImageIcon(image);
                JLabel imageLabel = new JLabel(ii);

                f.getContentPane().add(imageLabel);
                f.pack();
                f.setLocationByPlatform(true);

                f.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:0)

您可以考虑在图形环境中使用LineBreakMeasurer用于此类事情。请查看JavaDocs here,其中包含一些有关如何使用它的详细示例。