text in ImageTextButton bigger on higher resolution devices - libgdx

I am having some trouble specifying the size of text in ImageTextButton. I didn't found any method or property to define text size. I used the code below,but got very small text. I expect text to be bigger on higher resolution devices, since all my code is relative to the dimensions of screen. Is bitmap font the correct approach? How does one code text in ImageTextButton to be bigger on bigger devices?
font = new BitmapFont(Gdx.files.internal("font.fnt"));
ImageTextButton.ImageTextButtonStyle bts = new ImageTextButton.ImageTextButtonStyle();
bts.up = ninePatchDrawable;
bts.font=font;
bts.fontColor= Color.WHITE;
checkButton=new ImageTextButton("Text",bts);
checkButton.setBounds(Gdx.graphics.getWidth() * 0.2f, Gdx.graphics.getHeight() * 0.01f,Gdx.graphics.getWidth() * 0.24f,Gdx.graphics.getHeight()*0.08f);
stage.addActor(checkButton);

Turns out I had to choose different keywords for google search. This solves my problem How to draw smooth text in libgdx?

Related

Difference in font size on tablets and phones

I really hope, you are able to assist me on this one, as I'm tearing my hair out...
I have a little marquee, based on this code: http://jsfiddle.net/TCJT4/525 that feeds some text.
Here's how it looks on an iPad 6... and please disregard from the preliminary design, but this is how it should look:
Here's how it looks on an iPhone 4S:
The ticker is retrieved from the exact same source, but as you can see, the text appears larger on the iPhone (the iPad image is zoomed, so it appears larger, but in reality, they are both displaying a 320x30 pixels placeholder. The text is temporarily hardcoded to 20px in height and I've tried using other units as well... the banner still looks different on the devices.
I did some debugging of the ticker container/placeholder, as well as the detected banner height and disabled all text-adjusting elements. Here's a result of some of the properties:
iPad 6: Tickerplaceholder DIV-height: 24pixels, bannerheight: 30px, pixelaspect-ratio: 2
iPhone: Tickerplaceholder DIV-height: 32pixels, bannerheight: 30px, pixelaspect-ratio: 2
PC (Chrome): Tickerplaceholder DIV-height: 24pixels, bannerheight: 30px, pixelaspect-ratio: 1;
I find it very strange that two retinadisplay devices display the same banner differently - and that the iPad and the PC displays them correctly.
The ticker can also be found here in its latest form: www.videobanner.dk/ph.html
Pixels are different physical sizes on different devices - so 24px is smaller on one device than on another.
For text, if you use points instead then the size will be the same across devices - they will all make 72pt 1 inch (thereabouts).
Of course this means you have to use text and not bitmaps etc.
Mobile devices may also have a zoom level set for readability (by the user) which will also affect the size - eg you specify 24pt or px and the browser makes it 36pt or px - the calculated size in the inspector will be different to the styled size - to get around this you need to set a value somewhere, then see what it actually is when rendered and apply a ratio to get what you want (via javascript). I've used code like this in the past to ensure text fitted in a box of a given pixel size;
var fontScale = 1 ;
var mySpecifiedFontSize = 24 ;
var myTextElement = document.getElementById("MY_TEXT_ELEMENT_ID") ;
function fontScalingCorrection(){
var style = window.getComputedStyle(myTextElement);
var fontSize = parseInt(style["font-size"]);
if(!isNaN(fontSize)){
if(fontSize !== mySpecifiedFontSize){
fontScale = (mySpecifiedFontSize / fontSize) * fontScale ; //allows for multiple calls
myTextElement.style.fontSize = (fontScale * mySpecifiedFontSize) + "px" ; //or units used
}
}
}
//after the element has been drawn once ( or use another element as a size marker )
fontScalingCorrection();
The cause of the problem is related to a quirk or error in iOS Safari, which returned an incorrect and unpredictable height when dealing with unordered lists, containing text of various lengths. This became apparent when I compared different text lenghts on different platforms. No fix has been found, but I was able to circumvent the problem by splitting one string into several shorter strings, such as
<li>This is a text that </li><li>doesn't go well with iOS</li>
In my project, this solution also works... perhaps not that pretty, though.

How to get the natural fontsize of bitmapfont?

If I have a bitmapfont such as;
fonttexture = new Texture(Gdx.files.internal("data/fonttest.png"), true);
font= new BitmapFont(Gdx.files.internal("data/fonttest.fnt"),
new TextureRegion(fonttexture ), true);
Is it possible to retrieve or deduce the natural fontsize used on the associated png texture file?
The *.fnt file itself specifies this on the first line;
info face="Calibri Light" size=32 bold=0 italic=0 charset=""
Its that 32 I am after in code.
I couldn't see a method on BitmapFonts class to directly get it, but there was various other height related attributes possible to get.
I tried recreating the size from those but didn't have much luck.
float baseToAssent = font.getCapHeight()+font.getAscent();
float baseToDecent = -font.getDescent();Decent);
float fontHeight = baseToAssent+baseToDecent; //too small, like 11 not 32
Not being a fonttographer? fontophile?...umm...graphic designer, I am probably putting the pieces together wrong, or maybe just lacking one.
In either case, any way to work out the font size used in the fonttest.png would be helpful.
Thanks
Usecase;
I am manually using that texture with a distance field shader. Knowing the textures natural font size is useful in order to accurately scale the text and lineheight to any requested font size without ever resizing a image and thus losing quality.
Not sure how to get the font size specific, however you can easily get the String width and height of a font using a GlyphLayout, like this:
BitmapFont font = new BitmapFont(Gdx.files.internal("data/fonttest.fnt", false);
GlyphLayout glyph = new GlyphLayout(font, "TextMessage");
float width = glyph.width;
float height = glyph.height;
Read this topic if you want to learn more about GlyphLayout and BitmapFont.
Experimented quite a bit with a 32px font, like yours. The String "W" seemed to be 29px wide, which was the widest I could find, so it's definately not precise if what you want to archive is the size exactly.
You could do a little hacky method, reading the .fnt file as a text file and looking for the Integer after "size=", pretty much like a properties file work, it will be less efficient but will return exact value.

Text scaling and positioning in libgdx

I am working with libgdx. I need to scale and position text. Let's say I want to draw X that is 30 pixels hight and I want it to be in the middle of the screen. I want to draw more of those in diffrent locations and with different scales.
Is there any way how could I achieve that? I can't find the solution anywhere. I dont want to create more BitmapFonts if possible.
If you want to handle all platforms (android, html, ios, desktop) you need to use several BitmapFonts in order to avoid ugly scaling. Otherwise, if you don't need to deploy to HTML, you can use the gdx-freetype extension (see here https://github.com/libgdx/libgdx/wiki/Gdx-freetype).
Assuming you go with BitmapFont, you can simply use code similar to this to center your text:
String text = "Your text here!";
TextBounds bounds = font.getBounds(text);
font.draw(batch, text, (width - bounds.width) / 2.0f, (height - bounds.height) / 2.0f);
For scaling, you can set the scale in font.draw, but you probably want several BitmapFont of various sizes to avoid ugly artifacts.

Any way to resize text on the back of Windows Phone 8 Live Tiles?

I'm using a Flip Template for my tiles. On the MSDN page the text is seen to wrap, as well as be a smaller size.
This is not representative of behaviour I'm seeing in my live tiles. I can get a maximum of 3 lines, of large text, on the Wide live tile. The text does not wrap. This happens on all the different screen sizes of emulators. Frustratingly, I can get 4 lines of text on the Medium live tile, but the additional line of content is too long to fit there anyway so I don't include it.
I update my tiles periodically using a scheduled task:
Earthquake latest = quakes.First();
newTileData = new FlipTileData
{
Title = String.Format(AppResources.LiveTileTitleFormat, quakes.Count),
BackTitle = String.Format(AppResources.LiveTileTitleFormat, quakes.Count),
BackContent = String.Format(AppResources.LiveTileBackContentFormat, latest.FormattedMagnitude, latest.FormattedDepth),
WideBackContent = String.Format(AppResources.LiveTileWideBackContentFormat, latest.FormattedMagnitude, latest.FormattedDepth, latest.RelativeLocation)
};
ShellTile tileToFind = ShellTile.ActiveTiles.First();
if (tileToFind != null)
{
tileToFind.Update(newTileData);
}
The emulator on the left is attempting to show 4 lines.
The emulator on the right is showing the text not wrapping.
So, is there any way to force a fourth line, or specify a smaller font size, or both? I suspect there isn't, and the MSDN article is simply showing Windows 8 (not WP8) live tiles.
It's not possible to resize text on WP8 tiles. You can fake it by creating an image and placing that on the tile instead:
Create a user control that accommodate an image control and Text block
Assign you tile image to image control and the text with desire size to text block.
Then create a snapshot of the control through following code.
use this image for implementing tile generation.
private WriteableBitmap RenderControlAsImage(UserControl element)
{
element.UpdateLayout();
element.Measure(new Size(element.Width, element.Height));
element.Arrange(new Rect(0, 0, element.Width, element.Height));
return new WriteableBitmap(element, null);
}

Autoscaling TLF Text in AS3

I'm needing an actionscript solution that will allow dynamic text to drop into a text box with pre-determined dimensions (x, y, width, height), and then will scale the text up or down so that it is as large as it can be within those dimensions without scrolling. Wordwrap would be automatic, and there would not be any paragraph breaks.
I have a working model using Flash's Classic text, but I would like to be able to utilize the in-line styling that TLF provides. I just don't quite have my mind wrapped around all the TLF features yet.
Does anyone know if there is an already existing solution to this situation - or could perhaps steer me in the right direction?
#phil: This should help: http://aaronhardy.com/flex/size-text-to-container/
Online demo, right click for source code.
Hm - this should work, but I am not sure over how precise the TLF font size is... Anyways:
newFormat:TextFormat = new TextFormat();
newFormat.size *= myText.width / myText.textWidth;
myText.setTextFormat(newFormat);
Now - this basically creates a TextFormat object and sets it's font size to myText's (the TextField) container width (the maximal width) divided by the actual text width. Again - if the TLF font size is NOT so precise, the size line must be:
newFormat.size *= Math.round(myText.width / myText.textWidth * 100) / 100;
100 means it is rounded to hundredths.
edit: I really believe this method is not only much simpler, but also efficient... I mean - that is the point of TextField.textWidth...