Centering fonts in VB6 - string

How do you determine the length of a string of text in Arial Bold font, and then center it in VB6?
We are not using a "label" or "picture box" to print the text to the screen. We are sizing the text on the fly, and allowing the user to scale the size of our application to their liking. We write the text to screen using code.

One way is to have a hidden picture box and setup the font specs of that picture box the way you want.
Then use the TextHeight and TextWidth methods of the PictureBox to take your measurements. The Units will be in whatever scalemode the Picture Box is set to.
If you are printing directly to the printer or form then just set your font FIRST then take your measurements.
To center it
MyText = "Hello World"
<displayarea>.FontName = "Arial"
<displayarea>.FontSize = 14
<displayarea>.FontBold = True
TextWidth = <displayarea>.TextWidth(MyText)
TextLeftCoordinate = <displayarea>.ScaleLeft+<displayarea>.ScaleWidth/2-TextWidth/2
<displayarea>.CurrentX = TextLeftCoordinate
<displayarea>.Print MyText
Substitute displayarea with whatever object you are using.
Based on your updated answer note that the hidden picture box suggestion isn't used to print. It is only get text measurement. However you are printing directly to the form so you just need to use the code example above.

I can't remember the specifics (it's been about 3 years since I last used VB 6), but there's a method on Form called something like "MeasureString". It takes the string, and measures it according to the font settings of the form.
Also, here's a comment posted by Jason Lepack in case I've misunderstood and over-complicated your requirements:
"Labels usually have an alignment property. If you set it to align to center then, regardless of the font face it should center in the label".

There are Win32 GDI functions you can invoke: see for example GetTextExtentPoint32 at http://msdn.microsoft.com/en-us/library/ms534223(VS.85).aspx

Your best option may be Form.TextWidth, which appears to return the width of a string in twips. I've just taken this approach in order to dynamically size a button based on the length of the label that needs to appear inside it.
There is also a corresponding function called Form.TextHeight which would allow you to do the same thing in the vertical dimension.
Make sure that you set the Font property of the form to match the Font property of the control you're intending to measure the text for, otherwise you'll get incorrect results.
Read more at http://msdn.microsoft.com/en-us/library/aa267168(VS.60).aspx

Related

Display text in a specified rectangle with directwrite

I want to display text with directwrite in a specified rectangle and with a specified font size, the text outside the rect should be cutted off.
By searching I found something about CreateTextLayout but it looked like it isn't possible to define the font size with it.
Thanks for answers.
It's not something DirectWrite is responsible for, unless you're using IDWriteBitmapRenderTarget. If you're using DrawText/DrawTextLayout from Direct2D you simply need to use D2D1_DRAW_TEXT_OPTIONS_CLIP option. If you are using DirectWrite bitmap target, it's enough to set target size to layout size, or to blit layout size portion to your destination context.
There is IDWriteTextFormat::SetTrimming(). When applied (with SetTrimming(&DWRITE_TRIMMING{DWRITE_TRIMMING_GRANULARITY_CHARACTER,0,0}, nullptr);), overflowing characters [glyph clusters?] and lines are omitted.

Put borders to a text mesh

I want to make a simple text box prefab. How can I keep the text in the textbox? Very simple question :)
Couldn't be easier
(1) Add a canvas
HINT always select "Scale with screen size" (Unity accidentally setthe wrong default there; you only ever use "Scale with screen size")
(3) Add UI -> Text
Set horizontal to WRAP
Look at the large yellow arrow
Leave the vertical as overflow (just choose a small height, say 10, for the box: it is irrelevant).
Set the width of the box to whatever you want - other large yellow arrow.
(In your specific example, it looks like you have a gray box, with a Text sitting on top of that gray box. You should simply make the text box expand to fit the gray box.)
Unity's reactive layout system is fantastic. If you're just getting started with it, search on here for QA and don't hesitate to post more questions. It is hard to master, but worth it.

In android how to set font size according to device screen size

How to change the font size of text displayed at run time in the styles.xml in my application depending on the screen size of the device used.
I had the same problem. I wanted to get the same aspect text ratio on every screen. No dp nor sp worked for me. The only solution that works for me is to create a dummy textbox (not visible) at startup then get the textsize and multiply for some ratio (depending on what you actually want). This value should be used to set text size dynamically to your controls.
At least it works for me because the same aspect remains on every different screen.
you need to add in the text size via dimens.xml files depending on screen size.....take a look at this following Question on SO - Text size and different android screen sizes
You can go through this, I used the below method to scale texts on different screen resolutions and sizes.
To define a range of text sizes and a dimension programmatically through the support library, call the TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(TextView textview, int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) method. Provide the text view on which it needs to be applied,the maximum value, the minimum value, the granularity value, and any TypedValue dimension unit (like TypedValue.COMPLEX_UNIT_SP).

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.

MailMerge: No fixed width font?

I am trying to send data to a specific MergeField. The data are sent correctly. Each line of the data has for specific characters. For example the data to the field may be:
12345 FIRST\nABCDE.F SECOND
(it cannot get the newline so i just so it through character \n)
Now in the printed document each character has its one width, '1' is smaller than 'E' for example. So the data are not alligned within the field. I tried the following fonts: Arial, Tahoma, Courier New. Nothing helped.
Any ideas? Thanks in advance.
Ps the data are sent through an executable built by Visual C++ 5.0!!
You should probably use a tab-stop based layout. Set your tab-stops every, say, centimetre or so (i.e. just big than the widest character in your font) and add a tab before each element that needs to be aligned.
With this you shouldn't need to find a fixed width font and can use something more attractive.
Edit: Out of interest, I wonder why you have no luck with Courier New which is fixed width.
Maybe you could post a screenshot somewhere so we can have a look at your problem in more detail.
Try Courier - it does not have kerning (kerning = variable character width)
Also in the Font window there is a check box that allows you to apply kerning to fonts of a certain size or above - setting this value to a large font size may remove kerning.

Resources