How to set the width of a textbox, depending on a specific format string? - textbox

I have a TextBox that uses a specific format string to display its content. The width of the box is set to take exactly the amount of space that is needed to display the text. For this purpose I use the width that the formatted text would have:
box.width = new FormattedText(
String.Format(Format, value),
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Arial"),
8,
Brushes.Black).Width;
When doing this the textbox almost has the right width. But I still need to add an offset of 12, otherwise the area of the text would be cropped by some pixels:
I got the number 12 by try-and-error. So two questions arise:
Why do I need this offset, were does the lost width go to (whitespace and border?)?
Is there a better approach to get the right offset than to just guess it?

Related

Why UILabel's height become zero if doesn't hold any text?

I expected from the label to keep the minimal height of one line even if you remove the text out of it.
Here's the picture:
How it should look (but without whitespace):
If I enter the one whitespace in it, its height gets corrected. But I dont want to put whitespace into empty labels. there should be a better solution.
The UILabel control have an intrinsic content size so if you don't have any text his height will be zero, If you need a min height then you must define a Height constraint with >= your minValue as I said in my comments
I think you can figured it out how do this
First, add a Width and a Height constraints:
Second, change the Equal to the Bigger Than(both for width and height, height could be small one, like 8):
Finally, you are able to change the font size of label freely, the label will became bigger with the bigger font size. And the label size will be (42,8) if you removed text:
One workaround is to use a UITextField instead, set isEnabled to false, and set the placeholder property to " ".
As long as you don't need it's width to ever be shorter than the width of a single space -- UITextFields' sizes are calculated using the placeholder property by default -- then this will always have the height of a non-empty UITextField regardless of whether it contains text or not.

Set numeric UILabel for Auto Layout to correctly compute the intrinsic content size

I have a UILabel with a font of size 50 and text 1. At runtime, its text is changed to other numbers.
If I, say, center it in its superview, the automatically detected (intrinsic content size) height is a lot bigger than the actual text, and this is because it tries not to crop other lower characters like g.
The thing is that I know I won't use other characters than digits. I also don't want to set a fixed height constraint for it.
UIFont metrics include ascender, descender, cap height, x height, etc... all of which determines how the characters fit into a container. There is a good explanation and diagram here: http://cocoanetics.com/2010/02/understanding-uifont
If you really want to get the height (and/or width) of the individual character "glyphs" you'll need to use Core Text. This will include calling CTFontGetGlyphsForCharacters() and CTFontCreatePathForGlyph() to get the "glyph path" (a CGPath object), at which point you can get the "bounding box" to determine the exact size.
Lots of discussions and example code out there... A good starting point is simply searching for CTFontCreatePathForGlyph

How to dynamically calculate the number of characters a UILabel holds before it gets truncated?

I have a UILabel that is configured with dynamic parameters:
textLabel.font = UIFont.systemFontOfSize(some_dynamic_size, weight: some_dynamic_font_weight)
textLabel.numberofLines = some_dynamic_number_lines
textLabel.frame = CGRectMake(0, 0, dynamic_width, dynamic_height)
How to calculate the number of characters this UILabel can hold before it gets truncated?
Calculate how tall the label would want to be given its font, text, and width. If that is taller than you want to make it, the text won't all fit.
The issue is that your label will be able to fit more or less text given on the font that you use and the size that it is set too. If you set that to a standard size say for instance San Francisco at 12pt then you will still have a varying amount of letters that can fit because not all letters are the same width for instance 'WWW' is allot bigger than ' lll ' but you could determine a "best estimate" by taking the average letter width. Then dividing the size of the label by that then you would have a general idea of how many letters could fit but you also need to account for the '...' that is inserted.
But I would suggest not concatenating at all and just make the text a variable size so it can shrink and show the whole word. I suggest looking into 'dynamic text' but I don't know what your application is so that may not be the best suggestion. Hope that helps 😜

iOS: Modify a font height, width, and spacing independently when creating PDF

In an iOS app I need to use a specific font but each character needs to be taller, thinner, and the spacing closed up to fit correctly. Is it possible to stretch/squish a font programmatically?
When you're adding text to a PDF file there are multiple ways to influence how the text is going to appear. The most generic way (and the way that might actually be sufficient for you) is to scale the text matrix:
void CGContextSetTextMatrix ( CGContextRef c, CGAffineTransform t );
As mentioned in the comment by #mkl, you can provide a matrix that will scale up in the Y direction while scaling down in the X direction. The effect will be that the letters are stretched out vertically and squished horizontally.
Normally I would expect you don't have to touch the spacing in that case, as spacing will be "squished" together just as the other characters.
Just in case that isn't sufficient, PDF actually does provide a way to change the spacing between characters too:
void CGContextSetCharacterSpacing ( CGContextRef context, CGFloat spacing );
While Apple's description talks about "additional space" to add between characters, the PDF specification and I suspect Apple's implementation as a result allows the spacing value to be both positive and negative. A negative value would have the effect of moving the characters closer together.
Seems like the best option would be to create your own custom font.
You are able to change the kerning of your font (the space between the letters) and the thickness/thinness of the font, however you probably aren't able to edit the height of the font, unless you edit the bounding box the font is inside of to scale the letters differently.
You might also want to consider using a different font...or if you're REALLY hardcore you can edit the font yourself using photoshop/illustrator.

How can I know the line number of editfield of blackberry

I would like to know the line number in EditField while I am typing some text in this component.
Get the width of the edit field. (int returned) (e.g. 320 px)
Get the font width of your current text. Font class have getAdvance() method that accepts the string parameter (int returned) (e.g. 650 px)
if string width > edit field width you can divide string width and edit width and on the other step get the modulo of it. (here you will get 2 as division and 10 will be yr modulo. That means you have filled two lines + in third line you are having characters that can be fit into 10 px.
I hope it will solve your problem, but be careful if you use any long words, the module had never been tested with long words but I fear about result's accuracy with long words.

Resources