Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计数,给定文本字符串将占用多少行_Java_String Formatting - Fatal编程技术网

Java 计数,给定文本字符串将占用多少行

Java 计数,给定文本字符串将占用多少行,java,string-formatting,Java,String Formatting,我有一个文本字符串,关于字体和行宽度的信息(以像素为单位)。是否可以计算,有多少行将占用这个字符串 If a word is long, it will be writen in a next row, for example: some test string | very_very_long_word | and even if there will be many white space so very_very_long_word <= this can't be on

我有一个文本字符串,关于字体和行宽度的信息(以像素为单位)。是否可以计算,有多少行将占用这个字符串

If a word is long, it will be writen in a next row, for example:
some test string    |
very_very_long_word |

and even if there will be many white space

so very_very_long_word <= this can't be on one row
so                  |
very_very_long_word |

and if the word is too long, it will be separated at the end of row:
veryveryverylonglong|
word                |
如果一个单词很长,它将被写入下一行,例如:
一些测试字符串|
非常长的词|
即使会有很多空白

所以very_very_long_word可以使用不同的类

使用图形2d

FontRenderContext frc = (( Graphics2D )g).getFontRenderContext();
String s = range.displayName;
float textWidth = (float) font.getStringBounds(s, frc).getWidth();
float textHeight = (float) font.getStringBounds(s, frc).getHeight();
或者只使用FontMetrics

FontMetrics fm = c.getFontMetrics(font);
int fontHeight = fm.getHeight();
int w = fm.stringWidth("yourstring");

没有办法通过某种方式做到这一点,所以我自己做到了:

public class LineCalculator {

private static final FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
private static  Font font;
private static int lines;

public int calcLines(String str, int width, String fontFamily, int scale)
{
    font = new Font(fontFamily, Font.PLAIN, scale);
    String[] source = str.split(" ");
    String line = "";
    lines = 1;

    for (int i = 0; i < source.length - 1; i++) {
        source[i] += " ";
    }

    for (String word : source) {
        if (font.getStringBounds(line + word, frc).getWidth() > width) {
            lines++;
            line = singleWordCheck(word, width);
        }
        line += word;
    }
    return lines;
}

private String singleWordCheck(String str, int width)
{
    CharSequence chars = str;
    for (int i = 1; i < chars.length(); i++) {
        if (font.getStringBounds(chars.subSequence(0, i).toString(), frc).getWidth() > width) {
            lines++;
            return singleWordCheck(chars.subSequence(i, chars.length() - 1).toString(), width);
        }
    }
    return str;
}
公共类行计算器{
私有静态最终FontRenderContext frc=新FontRenderContext(新仿射Transform(),true,true);
私有静态字体;
私有静态int行;
公共整数刻度线(字符串str、整数宽度、字符串fontFamily、整数刻度)
{
字体=新字体(fontFamily、font.PLAIN、scale);
String[]source=str.split(“”);
字符串行=”;
直线=1;
for(int i=0;iwidth){
行++;
行=单字检查(字、宽度);
}
行+=字;
}
回流线;
}
私有字符串单字检查(字符串str,int-width)
{
CharSequence chars=str;
对于(int i=1;iwidth){
行++;
返回singleWordCheck(chars.subSequence(i,chars.length()-1.toString(),width);
}
}
返回str;
}

}

这是固定宽度字体吗?否则,这将是非常棘手的。阅读这里:已经,但它只能给我文本宽度不依赖于它是一个单词字符串还是多个单词字符串。这将给我字符串的宽度,但正如我所说,如果我有一个30像素的行,35像素的单词将在30像素处分开,但是:3个15像素的单词将需要3个字符串,而不是2个!