I'm implementing multiline Label Figure.
I have a question.
How can i change font size of textflow ?
I tried with the method [textflow.setFont] before I had changed height value of fontdata.
use this code,Font tFont = m_Textflow.getFont();
FontData[] tFontDataList = tFont.getFontData();
tFontDataList[0].setHeight(aSize);
m_Textflow.setFont(new Font(null, tFontDataList[0]));
But that didn’t work correctly and made any space on head.
Help me please T^T
See the Font API. You'll find the constructor
Font(String name, int style, int size) -
Creates a new Font from the specified name, style and point size.
So you could do something like
String family = textFlow.getFont().getFamily();
int style = textFlow.getFont().getStyle();
int theNewFontSize = 30;
textFlow.setFont(new Font(family, style, theNewFontSize));
Related
I'm trying to change my JLabel text size using this code:
my_font = my_label.getFont();
my_font = my_font.deriveFont(
Collections.singletonMap(
TextAttribute.SIZE, TextAttribute.20));
There is a compiler warning on this line:
TextAttribute.20;
This code works perfectly and changes the text weight:
font_bold = numero_fattura.getFont();
font_bold = font_bold.deriveFont(
Collections.singletonMap(
TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD));
Question: How do I use the same code to change the text size?
As I understand your question, your answer is similar to this below said thread first answer, just follow this link -
How to change the size of the font of a JLabel to take the maximum size
Font labelFont = label.getFont();
String labelText = label.getText();
int stringWidth = label.getFontMetrics(labelFont).stringWidth(labelText);
int componentWidth = label.getWidth();
// Find out how much the font can grow in width.
double widthRatio = (double)componentWidth / (double)stringWidth;
int newFontSize = (int)(labelFont.getSize() * widthRatio);
int componentHeight = label.getHeight();
// Pick a new font size so it will not be larger than the height of label.
int fontSizeToUse = Math.min(newFontSize, componentHeight);
// Set the label's font size to the newly determined size.
label.setFont(new Font(labelFont.getName(), Font.PLAIN, fontSizeToUse));
hope this will help you, thanks.
I have a JTextPane with content type "text/html". It is integrated in a JScrollPane.
The user can scroll down in this JTextPane and hits a button. At this moment I want to compute the topmost actual visible line of the JTextPane!
What I found in another post here where these lines:
public Integer getActualDisplayedRows() {
int y1 = jtextpane.getVisibleRect().y;
int lineHeight = jtextpane.getFontMetrics(jtextpane.getFont()).getHeight();
int topMostRow = (int) Math.ceil((double) y1 / lineHeight);
return topMostRow;
}
But this does not compute correct.. The number in lineHeight is too small. So, if I scroll to the 20th row -for example- the method returns more then 20..
I tried to set the height of the line via stylesheet (like here):
StyleSheet sh = editorKit.getStyleSheet();
sh.addRule("body {line-height: 50px}");
But doesn't matter what pixel number I set there, the resulting JTextPane has always the same height (and I am using the body tag)..
Do you have any suggestions??
Thank you very much for your help!
If I understand your requirement you just want to know the line number at the top of the viewport?
Here is some code for getting the line at the caret position:
public static int getLineAtCaret(JTextComponent component)
{
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
return root.getElementIndex( caretPosition ) + 1;
}
Don't know if this will work for HTML with all kinds of weird tags with images and tables etc. In this case I'm not sure what the meaning of "line" would be.
Now obviously the caret will not be at the top of the viewport, so you need to modify the logic to get an "offset" of the text at the top of the viewport.
So you should be able to use the viewToModel(...) method of the text pane. Something like:
int y = textPane.getVisibleRect().y;
Point p = new Point(5, y);
int offset = textPane.viewToModel( p );
i have done a small test on LibGdx, on Multi-line Label, it seems that i cant get the wrapped line's height. Following is the code. Theoretically, height for aLebel should be > bLabel. But the result appear the same.
code:
aLabel.setText("this is a super long long long text that need wrapping."); // line wrapped into 3 lines
aLabel.setWrap(true);
aLabel.setWidth(470);
doLog("aLabel.getHeight(): " + aLabel.getHeight());
bLabel.setText("this is short."); // unwrapped line
bLabel.setWrap(true);
bLabel.setWidth(470);
doLog("bLabel.getHeight(): " + bLabel.getHeight());
result:
aLabel.getHeight(): 45.0
bLabel.getHeight(): 45.0
Do anyone have any idea how to get the actual multi-line height in LibGdx? Thanks in advance.
I had this issue for years and accidentally solved it by setting the width and packing the label twice. Note that multiline labels were never intended to figure out their own width, so you have to set them externally, preferably from it's parent.
public Label createLabel() {
// Create label and set wrap
Label label = new Label("Some long string here...", skin);
label.setWrap(true);
// Pack label
label.pack(); // This might not be necessary, unless you're changing other attributes such as font scale.
// Manual sizing
label.setWidth(textWidth); // Set the width directly
label.pack(); // Label calculates it's height here, but resets width to 0 (bug?)
label.setWidth(textWidth); // Set width again
return label;
}
LibGDX version used: 1.6.4
Pack sizes the widget to its pref size, nothing more. Pref width of a label with wrapping is 0.
Label label = new Label(...);
label.setWrap(true);
label.setWidth(123);
label.setHeight(label.getPrefHeight());
I had the same issue and it seems there doesn't exist a method in Label class to solve this. Also, I agree with you, the getHeight() method should return the real height of the Actor, so I don't know if that's a bug or there is a reasoning behind that behaviour.
Anyways, how I solved the issue is by using BitmapFont's getWrappedBounds method. It's not short, but for your example it would be the following:
doLog("aLabel.getHeight(): " + aLabel.getStyle().font.getWrappedBounds(aLabel.getText(), aLabel.getWidth()).height);
This could be done by adding a restriction to the cell that contains the Label in the Table:
Label label = new Label("Example", new Label.LabelStyle(font, Color.WHITE));
label.setWrap(true);
Table table = new Table();
table.add(label).width(WITH);
For more information about how to use Table go to: https://github.com/libgdx/libgdx/wiki/Table
I have to write app, which can scale font's size of label and buttons depending on size of entire frame. My idea is to increase size of font until whole text fits into label or button.
How can I check is all string fit to button/label?
Something like the following
JComponent c = ... // it can be label, button or any other component
FontMetrics fm = c.getFontMetrics(c.getFont()); // or another font
int strw = fm.stringWidth("My text");
I took this example from Getting string size in java (without having a Graphics object available)
Let's say I have a JButton, and I want it to be big enough to fit a string of 8 "M" characters, regardless of the string that is actually assigned to it and the font size, without using elipsis.
The JButton has to have precisely this size, no more, no less. Layout manager in use is GridBagLayout.
I tried overwriting the getPreferredSize() method and perform a calculation using the string and the current font of the system. The calculation gives me back some sensible value, however, I have no idea how to set the preferred size in such a way that the borders are also considered.
I tried to get the insets of the component, but they are all 0's.
This is the code of my method:
public void getPreferredSize() {
Dimension d = super.getPreferredSize();
// Geometry width indicates how many characters must fit
char[] pad = new char[propGeometryWidth];
Arrays.fill(pad, 'M');
String tmpTemplateString = new String(pad);
FontMetrics tmpMetrics = getFontMetrics(getFont());
Rectangle2D tmpR2D = tmpMetrics.getStringBounds(tmpTemplateString, getGraphics());
int tmpWidth = (int)tmpR2D.getWidth();
int tmpHeight = (int)(tmpR2D.getHeight() * propGeometryHeight + tmpR2D.getHeight());
// We need to take into consideration borders and padding!
Insets insets = getInsets();
Dimension tmpSize = new Dimension(tmpWidth + insets.left + insets.right, tmpHeight + insets.top + insets.bottom);
return tmpSize;
}
I get the feeling that this might be related to the fact that my component is not realized yet, but I am completely unsure how I could solve this issue. Am I approaching this problem from the wrong perspective?
I think you may actually be doing it right already. From the Javadoc for getInsets():
If a border has been set on this component, returns the border's insets; otherwise calls super.getInsets.
A freshly-created JButton for me shows insets of java.awt.Insets[top=5,left=17,bottom=5,right=17] with the default look and feel, and java.awt.Insets[top=4,left=16,bottom=4,right=16] with the Windows look and feel. Are you using a custom look and feel, perhaps?
I found the reason for my problem. The problem is that I had a panel with a JButton inside, and I overwrote the method on the panel (There is a relatively complex hierarchy of classes). Then, of course, the insets for the Panel are all set to 0. After getting the insets for the button, as stated by Mr. Mmyers, it all works great.