JTextarea with dynamic text length and wrapping in BoxLayout wrong height - height

I'm trying to have multiline labels and image labels in a veritcal BoxLayout. For the multiline labels I use a JTextArea with setEditable(false). For the image labels I use a JLabel([ImageIcon]).
The following code shows that the textarea has a lot of space below it and I don't want that. To keep it simple I added text labels instead of image labels.
What I want is to stack the textarea and the labels from top to bottom. After each textarea the label should follow immediately below and after the last label there should be empty space up to the bottom of the window.
Maybe another Layout Manager is better, but I think it is a JTextArea issue. Any solution would help.
Thanks.
here is the compilable code:
import java.awt.Color;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class BoxLay extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(final String[] args)
{
new BoxLay();
}
private BoxLay()
{
setTitle("BoxLayout TestDummy");
setSize(800, 450);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
final JTextArea area1 = new JTextArea();
area1.setText("First Text - Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... ");
area1.setLineWrap(true);
area1.setWrapStyleWord(true);
area1.setEditable(false);
area1.setBackground(Color.RED);
this.add(area1);
final JLabel label1 = new JLabel("DIRECTLY BELOW FIRST TEXT");
this.add(label1);
final JTextArea area2 = new JTextArea();
area2.setText("Second Text - Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... Dynamic text of any length... ");
area2.setLineWrap(true);
area2.setWrapStyleWord(true);
area2.setEditable(false);
area2.setBackground(Color.RED);
this.add(area2);
final JLabel label2 = new JLabel("DIRECTLY BELOW SECOND TEXT");
this.add(label2);
this.add(Box.createVerticalGlue());
this.getContentPane().invalidate();
this.getContentPane().validate();
}
}

Finally wrote my own LayoutManager (dejavu!). It simply stacks components from top to bottom and does not try to make them fit the whole screen. It's a prototype, though, and X_AXIS is not supported. Probably a lot can be improved, but maybe it helps someone. I would also be very happy, if someone can improve it an share it with me, because I'm still a beginner regarding LayoutManagers.
Anyway, here is the code:
public class StackLayout implements LayoutManager
{
static final int X_AXIS = 0;
static final int Y_AXIS = 1;
private final int axis = Y_AXIS;
public void addLayoutComponent(final String name, final Component comp)
{
}
public void removeLayoutComponent(final Component comp)
{
}
public Dimension preferredLayoutSize(final Container parent)
{
return new Dimension(200, 200);
}
public Dimension minimumLayoutSize(final Container parent)
{
return new Dimension(0, 0);
}
public void layoutContainer(final Container parent)
{
//TODO implement x axis layout
final Dimension size = parent.getSize();
if (axis == Y_AXIS)
{
final int left = 0;
int top = 0;
for (int i = 0; i < parent.getComponentCount(); ++i)
{
final Component component = parent.getComponent(i);
component.setSize(parent.getSize().width, 1);
final Dimension preferredSize = component.getPreferredSize();
component.setBounds(left, top, Math.max(preferredSize.width, parent.getSize().width), preferredSize.height);
top += preferredSize.height;
}
parent.setPreferredSize(new Dimension(parent.getSize().width, top));
}
}
}

Related

set text align to be the center of sf::text with multi lines in SFML

I have a sf::Text with multiple lines how can I set its content alignment in the center sf::Text?
with just single line i can handle it with setOrigin but with multiple lines i dont know how
Depends on what your object is. I would reccomend having the multiline object be a vector of multiple sf::Text objects (each Text object is a new line). You get the right size of the box you want your multiline text to be in and then you use origin on each line to center each text in its position. You can try something like this:
class MultiLine
{
public:
vector<sf::Text> texts;
int width;
int heigth;
int posx;//starting position of the multiline text block
int posy;
int heigthSpacing;
sf::Font font;
int widthSpacing;
MultiLine(int wid, int he) : width(wid), heigth(he)
{
//you can manipulate the size of the box manually via contructor or dynamically via the amount of lines and spacing between.
//this is example of contructor
//addvalues here, move to position here. position is important for later on as setorigin sets origin of point + pos.
//you can change the pos at any time later too, but the initial pos is important
}
void addLine(string text)
{
sf::Text tmp;
tmp.setCharacterSize(30);
tmp.setFont(font);
tmp.setString(text);
tmp.setPosition(posx+width,posy+texts.size()*heigthSpacing);
texts.emplace_back(tmp);
}
void centerText()
{
for(auto &v : texts)
{
v.setOrigin(v.getGlobalBounds().width/2, v.getGlobalBounds().height/2);
v.setPosition(posx+width/2,v.getPosition().y);
}
}
};

How to automatically increase/decrease text size in label in Qt

I have a Qt application where I have a textedit and a label. When a user presses the button, the textedit text should be displayed on label. For the label I have set few properties like word wrap is enabled and horizontal and vertically it is aligned center. Below is the screenshot :
Now I have to automatically adjust the size of the text in label so that if someone enters a large string, then it should fit inside the label, that means size of text should decrease. And if the text string is small, then size should increase automatically to fill up the complete label. Currently if I am typing it the large string, it looks like something:
As you can see, in the above image, text is moving out of the label. It should remain inside the label.
How to detect in application if the text is moving out of the label height & width. Then how to reduce the text size. I want the size to automatically increase if the string is small and decrease it string is large to fill up the complete label. Is there any class or something provided in QT. Any help or example please. Thanks.
EDIT: With the below code I am able to reduce the size of text to fit inside the label width but not able to make the text multi line.
QString string = ui->textEdit->toPlainText(); //Getting data from textEdit
ui->label->setAlignment(Qt::AlignCenter); //Aligning label text to center
QFont f("Arial",50); //Setting the default font size to 50
QFontMetrics fm(f);
ui->label->setFont(f); //Setting the font to the label
int width = fm.width(string); //Getting the width of the string
int size;
while(width >= 870) //870 is the max width of label
{
size = ui->label->font().pointSize()-1; //Reduce font size by 1
QFont newFont("Arial",size);
QFontMetrics nfm(newFont);
ui->label->setFont(newFont); //Set the new font with new size
width = nfm.width(string); //Get the new width
}
ui->label->setText(string);
You (S. Andrew) solved it a little bit different like I proposed (just a statement but not critics). You did the word wrapping by yourself.
I wrote a minimal complete application to check how the Qt internal word wrapping can be used for your problem:
// standard C++ header:
#include <iostream>
#include <string>
// Qt header:
#include <QApplication>
#include <QBoxLayout>
#include <QFrame>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QStyle>
using namespace std;
class Label: public QLabel {
public:
void layout();
QRect documentRect(); // borrowed from QLabelPrivate
protected:
virtual void resizeEvent(QResizeEvent *pQEvent);
};
QRect Label::documentRect()
{
QRect rect = contentsRect();
int m = margin(); rect.adjust(m, m, -m, -m);
layoutDirection();
const int align
= QStyle::visualAlignment(layoutDirection(), QLabel::alignment());
int i = indent();
if (i < 0 && frameWidth()) { // no indent, but we do have a frame
m = fontMetrics().width(QLatin1Char('x')) / 2 - m;
}
if (m > 0) {
if (align & Qt::AlignLeft) rect.setLeft(rect.left() + m);
if (align & Qt::AlignRight) rect.setRight(rect.right() - m);
if (align & Qt::AlignTop) rect.setTop(rect.top() + m);
if (align & Qt::AlignBottom) rect.setBottom(rect.bottom() - m);
}
return rect;
}
void Label::layout()
{
// get initial settings
QString text = this->text();
QRect rectLbl = documentRect(); // wrong: contentsRect();
QFont font = this->font();
int size = font.pointSize();
QFontMetrics fontMetrics(font);
QRect rect = fontMetrics.boundingRect(rectLbl,
Qt::TextWordWrap, text);
// decide whether to increase or decrease
int step = rect.height() > rectLbl.height() ? -1 : 1;
// iterate until text fits best into rectangle of label
for (;;) {
font.setPointSize(size + step);
QFontMetrics fontMetrics(font);
rect = fontMetrics.boundingRect(rectLbl,
Qt::TextWordWrap, text);
if (size <= 1) {
cout << "Font cannot be made smaller!" << endl;
break;
}
if (step < 0) {
size += step;
if (rect.height() < rectLbl.height()) break;
} else {
if (rect.height() > rectLbl.height()) break;
size += step;
}
}
// apply result of iteration
font.setPointSize(size);
setFont(font);
}
void Label::resizeEvent(QResizeEvent *pQEvent)
{
QLabel::resizeEvent(pQEvent);
layout();
}
int main(int argc, char **argv)
{
cout << QT_VERSION_STR << endl;
// main application
#undef qApp // undef macro qApp out of the way
QApplication qApp(argc, argv);
// setup GUI
QMainWindow qWin;
QGroupBox qGBox;
QVBoxLayout qBox;
Label qLbl;
qLbl.setFrameStyle(Label::Box);
qLbl.setFrameShadow(Label::Sunken);
qLbl.setWordWrap(true);
qBox.addWidget(&qLbl, 1);
QLineEdit qTxt;
qBox.addWidget(&qTxt, 0);
qGBox.setLayout(&qBox);
qWin.setCentralWidget(&qGBox);
qWin.show();
// install signal handlers
QObject::connect(&qTxt, &QLineEdit::editingFinished,
[&qTxt, &qLbl]() {
QString text = qTxt.text();
qLbl.setText(text);
qLbl.layout();
});
return qApp.exec();
}
Compiled and tested with VS2013 / Qt 5.6 on Windows 10 (64 bit):
When playing around with this test application, I recognized that the text fits not everytimes perfectly into the QLabel. I tried to improve the code exchanging QRect rectLbl = rect(); with QRect rectLbl = contentsRect();. This made it better but still not perfect. It seems there is some finetuning necessary (where the development starts to become effort). (See update at end of text.)
Actually, it would not be necessary to derive QLabel. In my first implementation, layout() was a function with QLabel& and const QString& as parameters.
After I got the font size management working, I intended to consider resize events also. Googling a little bit, I found the solution to apply event filters. However, event filters are called before the event is processed but I need after. Finally, I decided to inherit QLabel and to overload QLabel::resizeEvent() to keep things simple.
Btw. I noticed it is even not necessary to set
height eventually to a very large value
as I suggested in a comment earlier. It seems that QFontMetrics::boundingRect(const QRect &rect, int flags, ...) increases the height automa[gt]ically to keep required width when Qt::TextWordWrap is enabled.
Update:
#annacarolina encouraged me to investigate a little bit deeper into this issue that font size is sometimes choosen to large. Some debugging in Label::layout() uncovered that sometimes computed rect looked like unwrapped text where visual output was wrapped. This made me suspiciuous about correctness of the rectLbl. Thus, I started in qlabel.cpp on woboq.org but actually the Qt forum QLabel: Resize font to contentsRect provided the final hint which leaded me to QLabelPrivate::documentRect() (actually again on woboq.org where I already had looked for enlightment). Thus, I added a method Label::documentRect() to my class. This makes results much better (although I'm not fully convinced about "perfect").
In the following code, I am making a logic where I am first getting all the words in the string. Then I am appending the words in QList<QString> data and checking if the width of the appended words is smaller than then width of the label. If the width goes above the width of label then I break it using \n. So in this way I made a list of the sub strings whose total width is around the width of the label and saved it in the List. Then I am calculating the width of sub strings stored in the list and then decreasing its font size till its total width is less than width of the label. After this I am displaying it on the label.
QList<QString> data;
CountWords Word;
ui->label->clear();
QString string = ui->textEdit->toPlainText(); //Getting data from textEdit
QStringList count = Word.GetWords(string); //Here I get the list of words in string
ui->label->setAlignment(Qt::AlignCenter); //Aligning label text to center
QFont f("Arial",50); //Setting the default font size to 50
QFontMetrics fm(f);
ui->label->setFont(f); //Setting the font to the label
int size,fontSize;
QString temp = ui->label->text();
int last = count.size();
//Saving the words in QList
for(int i=0;i<count.size();i++)
{
temp.append(count[i]+" ");
size = fm.width(temp);
if(size > 870)
{
temp.append("\n");
data << temp;
//data.append(temp);
temp.clear();
}
if((last-1)==i)
{
subString.append("\n");
data << subString;
subString.clear();
}
}
//decreasing the font size
QList<int> wide;
for(int i=0;i<data.size();i++)
{
wide << fm.width(data[i]);
while(wide[i] >= 870)
{
fontSize = ui->label->font().pointSize() - 1;
QFont newFont("Arial",fontSize);
QFontMetrics nfm(newFont);
ui->label->setFont(newFont);
wide[i] = 0;
wide[i] = nfm.width(data[i]);
}
}
//Finally displaying it on label
QString labelData;
for(int i=0;i<data.size();i++)
{
labelData = ui->label->text();
labelData.append(data[i]);
ui->label->setText(labelData);
}
After struggling with this issue, I create DynamicFontSizeLabel and DynamicFontSizePushButton widgets. Hope it helps.
https://github.com/jonaias/DynamicFontSizeWidgets/
Thanks Scheff for some inspiration.

Firemonkey: Shrink text font to fit in TLabel

I am attempting to lower the font size of a TLabel if its text is to large to fit in the confines of the label. I didn't see any properties I could set on the label to achieve this, so I have tried writing my own method. My method works by using TCanvas.TextWidth to measure the width of the text in a label, and shrink the font until the width of the text fits within the width of the label.
void __fastcall ShrinkFontToFitLabel( TCanvas * Canvas, TLabel * Label )
{
float NewFontSize = Label->Font->Size;
Canvas->Font->Family = Label->Font->Family;
Canvas->Font->Size = NewFontSize;
while( Canvas->TextWidth( Label->Text ) > Label->Width && NewFontSize > MinimumFontSize )
{
NewFontSize -= FontSizeDecrement;
Canvas->Font->Size = NewFontSize;
}
Label->Font->Size = NewFontSize;
}
This works some of the time, however other times it does not shrink the font near enough. It seems as if the value I get from calling Canvas->TextWidth is a lot of times, much smaller than the number of pixels wide the label actually needs to be in order to fit the text.
Am I using Canvas->TextWidth incorrectly? Is there a better way to calculate the width of a string, or to re-size the font of a TLabel so its text fits within its demensions?
Edit:
In this case, I am passing in to my function, the TCanvas that my label is sitting in. I have tried using that TCanvas as well as Label->Canvas. Both give me the same number for text width, and both are short of the actual value in pixels needed to display the whole string.
The following code is taken from code that works in an FMX application, modified slightly to remove arrays that are being iterated through and declaring a variable locally to the function. It is being run in a TForm method. Canvas here is the Form's Canvas. You can see that I'm using "- 35" at one point - this might be because the numbers weren't quite right.
double InitialFontSize = 30;
Canvas->Font->Size = InitialFontSize;
StoryHeadlineLabel->Font->Size = InitialFontSize;
bool fits = false;
do
{
double widthA = Canvas->TextWidth (StoryHeadlineLabel->Text);
if (widthA > StoryHeadlineLabel->Width - 35)
{
StoryHeadlineLabel->Font->Size --;
Canvas->Font->Size --;
}
else
fits = true;
if (StoryHeadlineLabel->Font->Size < 6)
fits = true;
} while (!fits);

How do I prevent custom font in Cocos2d from appearing cut off

I am using a custom font called KomikaTitle. In some cases the font appears cut off on the left in the first character. This doesn't happen when I use a native font such as Arial.
The following is the code I am using:
scoreDisplayLabel = [CCLabelTTF labelWithString:#"0" dimensions:CGSizeMake(200,30) hAlignment:UITextAlignmentLeft fontName:#"KomikaTitle" fontSize:18];
scoreDisplayLabel.color = (ccc3(r,b,g));
[self addChild:scoreDisplayLabel z:2];
[scoreDisplayLabel setPosition:ccp(115,wins.height-73)];
How do I prevent this from happening? I am attaching a screenshot of the issue.
I tried messing around as suggested in http://www.cocos2d-iphone.org/forums/topic/custom-font-being-cut-off/, but no luck.
Thanks guys!
This maybe isn't a real answer, but I had the same problem with that font in an old cocos2d project I made. Just just added an extra space and a row.
This may or may not be related, but according to this source you have to include the file extension of your font. Where you have
fontName:#"KomikaTitle"
it should be
fontName:#"KomikaTitle.ttf"
for example.
If there are any android users out there using cocos2dx, this is not necessarily an easy problem to solve, but it is doable once you go down the rabbit hole. It does require editing the Cocos2dxBitmap.java file, which means that any changes made could be overrided by an update. Basically, the methods that are used to measure text are, while not incorrect, inadequate.
First, we need to add a new variable to the TextProperty
private final int mX;
Next, replace the computeTextProperty code with the following:
private static TextProperty computeTextProperty(final String pString, final int unusedWidth, final int unusedHeight, final Paint pPaint) {
final FontMetricsInt fm = pPaint.getFontMetricsInt();
final int h = (int) Math.ceil(fm.bottom - fm.top);
int maxContentWidth = 0;
final String[] lines = Cocos2dxBitmap.splitString(pString, 0,
0, pPaint);
/* Compute the max width. */
int temp = 0;
float left = 0;
for (final String line : lines) {
//get a path from text
Path path = new Path();
pPaint.getTextPath(line, 0, line.length(), 0, 0, path);
RectF bounds = new RectF();
path.computeBounds(bounds, true);
temp = (int) FloatMath.ceil(bounds.width());
//if the text extends to the left of 0
if (bounds.left < left) {
left = bounds.left;
}
if (temp > maxContentWidth) {
maxContentWidth = temp;
//extend the width to account for text rendered to the left of 0
if (left < bounds.left) {
maxContentWidth += (int) FloatMath.ceil(Math.abs(left));
}
}
}
left = Math.abs(left);
return new TextProperty(maxContentWidth, h, lines, (int) FloatMath.ceil(left));
}
What has basically happened is that we have used information returned by the text path to get if the left bound is less than 0, which would mean it would be rendered outside the bitmap. We also extend the width when there are multiple lines of text, as we are going to shift everything to match the left bounds, we need the right bounds shifted too.
Finally, replace computeX with
private static int computeX(final String pText, final int pMaxWidth,
final int pHorizontalAlignment, final int pX) {
int ret = 0;
int expectedWidth = pX + pMaxWidth;
switch (pHorizontalAlignment) {
case HORIZONTALALIGN_CENTER:
ret = expectedWidth / 2;
break;
case HORIZONTALALIGN_RIGHT:
ret = expectedWidth;
break;
case HORIZONTALALIGN_LEFT:
ret = pX;
default:
break;
}
return ret;
}
You'll have to do all the hookups yourself, but this will provide the most accurate text rendering.

How to get Words length and height, when drawing the images on a canvas in GDI+

I draw a string on a canvas, using GDI+ in C++.
Is there any API to get the out layer (width, height ) of a string in a certain font?
Thanks very much!
Many thanks to Windows programmer's solution.
I wrote the following code.
Bitmap bitmap(1000,1000);
Graphics graphics(&bitmap);
RectF rec;
RectF useless;
graphics.MeasureString(m_sWords, -1, m_pFont.get(), useless, &rec);
int WordWidth = rec.Width + 1;
int WordHeight Height = rec.Height + 1;
Need I use a real graphics to call MeasureString? Is there any way to get wordwidth,wordheight, without create a large Graphics Instance? I found it is resource comsuming.
Graphics::MeasureString computes an approximation.
To make the picture complete: it can simply be done with a GraphicsPath, which is better since it requires no DeviceContext:
Dim p As New GraphicsPath
Using stringFormat As New StringFormat()
stringFormat.Trimming = StringTrimming.EllipsisCharacter
stringFormat.LineAlignment = StringAlignment.Center
stringFormat.Alignment = StringAlignment.Near
p.AddString(text, font.FontFamily, font.Style, font.SizeInPoints, Point.Empty, stringFormat)
End Using
Return p.GetBounds.Size
Where text is a given string and font a given font. Returns a SizeF-Structure. I've found the results to be much more precise than Graphics.MeasureString aka GdipMeasureString-API.
Unfortunately you do need to use a Graphics object to do this.
The C# code I use (which returns a RectangleF, because I want to know both the width and the height) is as follows:
/// <summary> The text bounding box. </summary>
private static readonly RectangleF __boundingBox = new RectangleF(29, 25, 90, 40);
/// <summary>
/// Gets the width of a given string, in the given font, with the given
/// <see cref="StringFormat"/> options.
/// </summary>
/// <param name="text">The string to measure.</param>
/// <param name="font">The <see cref="Font"/> to use.</param>
/// <param name="fmt">The <see cref="StringFormat"/> to use.</param>
/// <returns> The floating-point width, in pixels. </returns>
private static RectangleF GetStringBounds(string text, Font font,
StringFormat fmt)
{
CharacterRange[] range = { new CharacterRange(0, text.Length) };
StringFormat myFormat = fmt.Clone() as StringFormat;
myFormat.SetMeasurableCharacterRanges(range);
using (Graphics g = Graphics.FromImage(
new Bitmap((int) __boundingBox.Width, (int) __boundingBox.Height)))
{
Region[] regions = g.MeasureCharacterRanges(text, font,
__boundingBox, myFormat);
return regions[0].GetBounds(g);
}
}
This will return a RectangleF of the size of the entire text string, word-wrapped as necessary, according to the bounding box specified as __boundingBox. On the plus side, the Graphics object is destroyed as soon as the using statement is complete…
As an aside, GDI+ seems to be pretty unreliable at this; I've found it to be quite buggy (see my question “Graphics.MeasureCharacterRanges giving wrong size calculations in C#.Net?”, for example). If you can use TextRenderer.DrawText from System.Windows.Forms, then do.