This question already has answers here:
How to calculate the pixel width of a String in JavaFX?
(4 answers)
Closed 8 years ago.
How does one calculate the number of visible characters in one row (without horizontal scrolling!) of a TextArea in JavaFX depending on the width of the area?
I use a monospaced font, so all chars have the same width. Is it possible to calculate the width of a font char by its font size?
Yes it's possible to calculate them.
Even with a custom font.
You can use the FontMetrics class.
Font font = Font.font("YourFont", 14);
FontMetrics fontMetrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(font);
double length = fontMetrics.computeStringWidth("The text ");
But I guess this is the wrong direction?
But if your font is monospaced you can compute the width of one character and
then just simply divide the width of the textArea by the width of one character and you get the amount of max characters per line.
double widthPerChar = fontMetrics.computeStringWidth("A");
double maxCharsPerLine = textArea.getWidth() / widthPerChar;
Related
I create a text in ZPL format using zebra font 0, which is a scalable font. I need to calculate the text width and height, however I didn't know the relation between ZPL font parameter and letter size in dots. The ZPL command used to create text are :
^FO15,19^A0,20,20^FDSample_text^FS
The parameter 20,20 supposed to be font height (h) and width(w) in dots on the manual, however when I use font type 0 and test it, the height and width (20,20) not equal to the size of letter in dots.
What is the unit of height and width font parameter for scalable font? Is there a formula to calculate text height and width in dots for all font size input?
This question already has an answer here:
Decimal point alignment in iOS UILabel during countdown?
(1 answer)
Closed 5 years ago.
I have a bunch of UILabels inside a vertical UIStackView. The stack is making all labels with the same width and height.
Each label has a float in it, like this:
But this is ugly aesthetically. I want to align the numbers by the point. Like this:
One of my problem is that these floats are in different labels.
These numbers use this to format:
[NSString stringWithFormat:#"%.2f", value];
I don't have a clue if even it is possible. So, I have no code to show. Is it possible to do that? How?
EDIT: The floats are left aligned on the labels. Ok, I can reduce the labels width and align them to the right, but I am wondering if there is another solution in code because reducing the labels's width will cause other problems on the interface, crapping the whole thing.
If, as your edit says, you don't want to set your labels' textAlignment to right (which is certainly the easiest solution), here's another solution. You need to do two things:
You need to use a font with monospaced digits. In iOS 9 and later, the system font does not use monospaced digits by default, but there are programmatic ways to get a variant of the system font that does use monospaced digits. You can use +[UIFont monospacedDigitSystemFontOfSize:weight:], or if you already have a UIFont object, you can get a monospaced-digits variant (if there is one) by adding an attribute to its font descriptor. See this answer for code to do this (in Swift). Unfortunately there is no way to do this in a storyboard or xib without code.
You need to add enough U+2007 FIGURE SPACEs to the beginning of each label's text to make them all the same length. For example, if they should all be five characters long:
NSString *text = [NSString stringWithFormat:"%.2f", value];
while (text.length < 5) {
text = [#"\u2007" stringByAppendingString:text];
}
label.text = text;
You can try to set the UILabel to right align, e.g. Something like:
label.textAlignment = NSTextAlignmentRight;
Or for the single digit before the point case you could prepend the Unicode FIGURE SPACE, U+2007, this is a space with the same width as a digit in fonts with equal width digits and so all numbers will be the same width.
If you really need to do that from code without reducing UILabel's width, you can subclass UILabel and override this method:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 0, 0, rightOffset};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
Of course you should use right alignment as well.
This question already has answers here:
Font size in pixels
(8 answers)
Closed 6 years ago.
I have UIFont.systemFontOfSize(16.0)
I want to know the font size pixels on html
As #CynicismRising answer:- Point sizes are defined as 1/72 of an inch. That is, a 72-point font is approximately 1 inch from the lowest descent to the highest ascent. So the maximum height of a glyph in a 72pt font is about 1 inch.
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 😜
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?