私は不思議はあなたのFont
を使用String
の幅をmesureとString
のbounds
を取得するには、画素Libgdxで文字列の幅を取得する方法は?
答えて
BitmapFontのAPI < 1.5.6
で私の文字列の幅を取得する方法を知っています、あなたは引き出すつもりです。
BitmapFont.getBounds(String str).width
あなたも描画するためのオフセット右のために高さを得ることができます。単に幅を高さに置き換えます。
さらに、複数行のテキストの場合は、境界を取得するためにgetMultiLineBounds(someString).width
を使用します。
BitmapFontのAPI> = 1.5.6
BitmapFontのAPIは、今境界を取得する別の方法がある1.5.7で変更:
BitmapFont.TextBoundsとgetBoundsが完了します。その代わりに、文字列をGlyphLayoutに与え、幅と高さのフィールドを使用して境界を取得します。同じGlyphLayoutをBitmapFontに渡すことでテキストを描画することができます。これは、グリフを以前と同じように2回レイアウトする必要がないことを意味します。
例:@Nates答えによると
GlyphLayout layout = new GlyphLayout(); //dont do this every frame! Store it as member
layout.setText("meow");
float width = layout.width;// contains the width of the current set text
float height = layout.height; // contains the height of the current set text
:https://stackoverflow.com/a/20759876/619673呼び出す方法
BitmapFont.getBounds(String str).width
は必ずしも適切な幅を返します!特にフォントを再利用しているとき。
font.drawWrapped(spriteBatch, "text", x_pos, y_pos, your_area_for_text, BitmapFont.HAlignment.CENTER);
場合:たとえばin the center of part of view port
のためのテキストを描画したい場合 、あなたは別の方法
BitmapFont.drawWrapped(...)
サンプルコードを使用することで、この問題を回避することができますUIでスキンを使用すると、GlyphLayoutに入力する正しいフォントを見つけるのが面倒です。
このケースでは、Labelのインスタンスを使用して私のためにすべてを把握し、ラベルの幅を尋ねます。
Skin skin = getMySkin();
Label cellExample = new Label("888.88888", skin);
cellExample.layout();
float cellWidth = cellExample.getWidth();
Table table = new Table(skin);
table.defaults().width(cellWidth);
// fill table width cells ...
テキストを自分で配置したい場合、これは答えではないが、UIのレイアウトが安定しており、細胞の実際の内容にあまり依存させるのに便利です。
また、複数の行テキストに対してgetMultiLineBounds(someString).widthを使用することもできます。 –
ありがとう!問題を解決しました – LeSam
libgdxの新しいバージョンで廃止されました。私はAPIでそれを見つけることができません....彼らの別の方法はそれを行うのですか? –