Swift and autolayout: proportional font size in a label - text

I have a storyboard with (among other things) a label. Its height is proportional to the screen height. But the text size in the label is always the same. I'd like to also have a proportional font size.
Does anyone know how to do this?
Thank you for your help.
Edit: Maybe I should specify that my label has both width and height proportional to the screen size (the bigger the screen is, the bigger the label is).
I did it programmatically but I'd like to know if it's possible to do it from the storyboard.

click the label and choose the size properties inspector on the right hand side and edit the font size. You can then choose a size of your choosing

Related

Scaling inside viewport

I Have a viewport and a scaled image inside like this:
both the images are scaled to 0.1 but the one inside the viewport is of less resolution
Is there anyway to fix this?
The Viewport has a size, that size sets the resolution at which the contents of the Viewport will be rendered. And it does not have to match the size at which it is displayed (it could be stretched/scaled).
With the ViewportContainer you can decide if it will automatically set the size of the Viewport or if it will stretch it (scaling the ViewportContainer will also result in stretchering). If you prefer to not use the ViewportContainer※, set the size of the Viewport in the Inspector or form code.
※: For example, you can display a ViewportTexture with a TextureRect, which will give you more control on how it is displayed. But it is less convinnient to setup.

Codename one - scrollable layout restrictions

I have done my own version of the PropertyCross Demo (provided in their demo section).
The problem I currently face is the size of the "Recent Search" area. While I have a non-scrollable container, I can easily define the preferred height. As the Box Layout adheres to the preferred size, all is well, with the little issue of not being able to scroll it and see more than one result:
recentSearchContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS)); recentSearchContainer.setPreferredH((int)(this.getContentP‌​ane().getHeight() * 0.1f));
Once I set the container to scrollable, the preferred height gets overwritten and takes up as much space as it needs, taking too much space from the BorderLayout Center piece above it.
How to manipulate the preferred size of scrollable components?
You don't manipulate the preferred size. Scrollables take up more space so if you need them to take up a specific amount of space you need to use the right type of layout which in this case might not be border layout...
Border layout gives NORTH/SOUTH elements their preferred height which might not be what you want. You might want a grid layout which will divide the height 50/50. You might want a table layout where you can define the height in percentages etc.
For those who are interested, here is the solution:
Setup a table layout with a single column and as many rows as you need (similar to box layout y axis or border layout which only north, center and south).
Set the table layout to non-scrollable so it defaults to 100% of your screen.
add the components with height % of the screen they should take up.
those components can be scrollable and will still stick to the height constraint!
// inside a form object, setup the layout
TableLayout tl = new TableLayout(3, 1);
tl.setGrowHorizontally(true);
setScrollable(false);
setLayout(tl);
...
// and add stuff to it
add(tl.createConstraint().heightPercentage(15), labelDesc);
add(tl.createConstraint().heightPercentage(50), compGroup);
add(tl.createConstraint().heightPercentage(35), recentSearchContainer);
Works like a charm!

How to get correct text width for oblique font in DirectWrite?

I create a IDWriteTextLayout object by the following code,
hr = g_pDWriteFactory->CreateTextLayout(text, textLength, *g_pTextFormat, 200000000, 200000000, g_pTextLayout);
then get the text width by text metric,
DWRITE_TEXT_METRICS dtm;
pTextLayout->GetMetrics(&dtm);
float minHeight = dtm.height;
float minWidth = dtm.widthIncludingTrailingWhitespace;
what confuse me is that whether the style of the font is DWRITE_FONT_STYLE_OBLIQUE or DWRITE_FONT_STYLE_NORMAL, the width of the same string is the same value. Why? I expect that when the font style is DWRITE_FONT_STYLE_OBLIQUE, the width should be bigger. how can i get the correct width for oblique text?
Thanks.
Layout metrics are based on font design metrics, either hinted or not, and not on actual ink box for each glyph. If you want metrics that closer match resulting rendered bitmap size, you can adjust returned layout metrics with data GetOverhangMetrics() returns. But at the end the only way to know exact pixel size of resulting layout is to render it.
So answering your question, returned layout metrics for oblique font are already correct, they just mean slightly different thing than you expect them to.

How to change the size of the font of a Label to take the maximum size

I have a Label in a Scene. I need to get always the largest font size for the text in it, so that the text always takes the maximum size in the available size of the label.
How can I do that?
final double MAX_FONT_SIZE = 30.0; // define max font size you need
label.setFont(new Font(MAX_FONT_SIZE)); // set to Label
I am not sure if we have a way to set font size according to the size of the container. But you could scale the text vertically or horizontally to fit the container bounds. Check out the following ...
Scaling a textbox without truncating text within it

How do I display same "proportional" text size on different resolutions?

I draw some text on screen using ID3DXFont::DrawText. This text should be displayed the same regardless of screen resolution.
For example, if screen resolution is low, text wrapped and when it is higher text is not wrapped. How can I avoid such situation? I want text size to be connected to screen resolution so if resolution is lower I want the text to be relatively smaller so that no wrapping happens. Is there any way?
Thanks in advance
Below is a logical solution. It doesn't have any of the code or procedures needed to make work in direct X but having done something similar outside of DirectX I wanted to share the logic
Working on whatever default screen resolution you want set the font to be the size needed.
Find the percentage of the screen height the font size you chose takes up.
Then having stored that value when you go to render again, in the final version of the program, calculate the font size based on the screen size and the percentage
What I was working in had functions like GetTextHeight and properties on the font size to allow you to set the height (which in turn set the font size appropriately). So if can find anything similar to this in DirectX than this could be a route for you to take.

Resources