TextField how to set width to hold certain number of characters - swiftui

is it possible to set the width of SwiftUI TextField to hold a certain number of characters and display them without cutting them off? This way the width would adjust automatically based on font size.
The
.frame(width/minWidth/maxWidth) is not really useful in this sense unless I would calculate the size of the characters.
Thanks for any ideas.
Libor

I may have found a solution and that is
TextField(....)
.fixedSize(horizontal: true, vertical: true)

Related

Motif How to calculate or retrieve the font pixel size?

I am trying to write code in Motif to change a dialog warning box to resize size it if the box is not wide enough. The width and height is always being set by the calling classes and its not always wide enough for the message being displayed and the end of the line is truncated off. Instead of fixing everywhere to use auto sizing (i.e. width is 0 or not set at all) they want to figure out what the pixel width size is for a character in the dialog. They can then multiple the longest line X pixels width to get the lines length in pixels. Then we would see if the dialog declared width needs to be reset to stop the truncation. Only dialogs that are too short will be changed (dialogs too wide are not to be changed).
However; I can't find any example on how get the character width in pixels anywhere. I remember years ago I was on a project where they created some type of widget, inserted a character into it, and then did a XtGetValues to get the width and height so I think it can be done. So does anyone know how to do this?
That was a long time ago, but if memory serves, Xt doesn't have any specific support for fonts, it relies on plain libx11. You will need to call XQueryFont or XLoadQueryFont to get the XFontStruct describing your font, then grovel through the per_char array to find the extents of individual glyphs.

SwiftUI scaleEffect and layout bounds

I want to create a Text view with slighter larger than normal font. I tried,
Text("Foo").scaleEffect(1.3)
The text is larger as desired, but the layout uses the original borders, so the "Foo" is going off the bottom of the screen. Is there a way make it use the new larger bounds for layout?
I know there are named fonts, so I could do:
Text("Foo").font(.title)
but I wanted to say "1.3 times bigger than .body".
Here is possible approach
Text("Hello")
.font(Font.system(size: UIFont.systemFontSize * 1.3))

How to make fixed width of column with Helvetica font?

Usually, I use iomanip and setw to make a column with a fixed width for output.
But it seems to be that it doesn't work with Helvetica font.
What to do with it here?
Helvetica is a proportional font which means that the letters have different widths.
BTW, for most GUIs, you'll need something more specialized than std::cout.
In order to make a fixed width column you will need to pad to get to the next column. This means adding up the widths of the characters and the spacing. Subtract this from the column width and this becomes your padding.
Many GUIs have functions for determining the pixel widths of a string of text.
An easier route, IMHO, is to use a grid type widget in your GUI. These have column capabilities that can be adjusted. Some have functions that will resize a column based on its contents. You only need to put the text into that column.
Another GUI technique is to use vertical "boxes" (sizers) for each column. Place your text into the box. Let the box figure out its alignment and padding.

How to get correct position in the std::string?

I am creating a custom single line edit control, with a custom font in win32 api on windows 7, the font is not a fixed width font, and I need to move caret according to the mouse click, The edit control is not empty and if I know the horizontal position of the mouse click within the window, how do I calculate the number of characters after which I need to move caret to ?
I really am out of ideas, if it was a fixed width font, I would have divided the horizontal mouse click position with average character width, that would have been simpler, doing the same with not a fixed width font, is prone to errors.
Given that it's a single-line control, you probably don't plan on working with immensely long input (at least normally). That being the case, one possibility would be to just store the character positions in an array (or vector, etc.) Then you can use (for example) a binary search in that array to find character positions. Of course, you can do the same even for longer strings--though it can increase storage requirements quite a bit.
This is a familiar problem. You are in essence trying to do hit testing on text and for that you need the location on the screen of each character of the text.
My preferred strategy is to calculate an array of RECT, one for each character of displayed text. The array needs to be updated when text is added or deleted, but it easily handles single or multiple lines. The function GetCharWidth32 retrieves all the widths for a string of text in a particular font selected into a DC. For single line one call is enough, and calculating the array of RECTs is simple. It's not much harder to do multiline.
Handle the mouse down message, loop through the array and find the right character. A brute force search is plenty fast enough.
This method is simple and easily generalises to a range of similar problems.

bold and normal font rendering

It is observed in some fonts sat Verdana, that the rendering for bold and normal text does not occupy the same width for a given text.
In my application i am making use of one such font ,and there is UI with list and highlighted item.
for highlighted item the font is same with bold attribute ,because of above mentioned font issue the text appears to move(enlarge and occupy more space )horizontally when the list is scrolled.
I can use bold font with one less in font size which seem ok but not perfect
Is there any corrective measure to resolve this?
What you describe is the default attribute of most proportional fonts. If having the string width increased seems ugly, try using other visual methods to draw attention to the selected item, such as changing the background color.
You could make the list items wide enough to accommodate size increase when it becomes bold.
If the highlight has to be bold, most likely your only option is using a proportional font (doesn't need to be courier, you may find some sans-serif proportional fonts acceptable).