Page 1 of 2
Justify text in label
Posted: Fri Mar 26, 2021 11:56 pm
by danielmaximiliano
Hola a todos:
el control label tiene propiedades como ser "RIGHTALIGN" y "CENTERALIGN"
hay alguna forma de justificar el texto ? mire dentro de C:\HMG.3.5\SOURCE\c_label.c pero encontre que no...
Hello everyone:
the label control has properties such as "RIGHTALIGN" and "CENTERALIGN"
is there any way to justify the text? I looked inside C: \ HMG.3.5 \ SOURCE \ c_label.c but found that no ...
Re: Justify text in label
Posted: Sat Mar 27, 2021 12:41 am
by Claudio Ricardo
Hola...

- Screenshot_20210326_213850.png (39.19 KiB) Viewed 1558 times
Code: Select all
DEFINE LABEL Label_AcercaDe_2
ROW 60
COL 205
WIDTH 210
HEIGHT 20
VALUE "Version 1.0 (04/2019)"
FONTNAME "Arial"
FONTSIZE 11
TOOLTIP ""
FONTBOLD .F.
FONTITALIC .F.
FONTUNDERLINE .F.
FONTSTRIKEOUT .F.
HELPID Nil
VISIBLE .T.
TRANSPARENT .F.
ACTION Nil
AUTOSIZE .F.
BACKCOLOR NIL
FONTCOLOR NIL
CENTERALIGN .T.
END LABEL
Re: Justify text in label
Posted: Sat Mar 27, 2021 1:25 am
by danielmaximiliano
centrar no es justificar
Re: Justify text in label
Posted: Sat Mar 27, 2021 3:18 am
by Claudio Ricardo
Hola...
Lo sé, pero para lograrlo habría que hacer una función que tomando el width del label y
el tamaño de la fuente, cuente caracteres más espacios, y divida la frase aún en medio
de una palabra, insertando un CRLF cada vez.
Alignment Left es lo más aproximado a eso, sólo que no corta las palabras.

- Screenshot_20210326_235600.png (30.59 KiB) Viewed 1541 times
Cuando tenga un dia libre intentaré hacer la función, mientras dejo preparado
un programita para probarla o si alguien del foro desea hacerla.
Pdt. Le puse fondo blanco para que se note mejor

Re: Justify text in label
Posted: Sat Mar 27, 2021 12:16 pm
by danielmaximiliano
Hola a todos : Encontre algo pero es C# pero muestra que se puede..
Code: Select all
public void Justify(System.Windows.Forms.Label label)
{
string text = label.Text;
string[] lines = text.Split(new[]{"\r\n"}, StringSplitOptions.None).Select(l => l.Trim()).ToArray();
List<string> result = new List<string>();
foreach (string line in lines)
{
result.Add(StretchToWidth(line, label));
}
label.Text = string.Join("\r\n", result);
}
private string StretchToWidth(string text, Label label)
{
if (text.Length < 2)
return text;
// A hair space is the smallest possible non-visible character we can insert
const char hairspace = '\u200A';
// If we measure just the width of the space we might get too much because of added paddings so we have to do it a bit differently
double basewidth = TextRenderer.MeasureText(text, label.Font).Width;
double doublewidth = TextRenderer.MeasureText(text + text, label.Font).Width;
double doublewidthplusspace = TextRenderer.MeasureText(text + hairspace + text, label.Font).Width;
double spacewidth = doublewidthplusspace - doublewidth;
//The space we have to fill up with spaces is whatever is left
double leftoverspace = label.Width - basewidth;
//Calculate the amount of spaces we need to insert
int approximateInserts = Math.Max(0, (int)Math.Floor(leftoverspace / spacewidth));
//Insert spaces
return InsertFillerChar(hairspace, text, approximateInserts);
}
private static string InsertFillerChar(char filler, string text, int inserts)
{
string result = "";
int inserted = 0;
for (int i = 0; i < text.Length; i++)
{
//Add one character of the original text
result += text[i];
//Only add spaces between characters, not at the end
if (i >= text.Length - 1) continue;
//Determine how many characters should have been inserted so far
int shouldbeinserted = (int)(inserts * (i+1) / (text.Length - 1.0));
int insertnow = shouldbeinserted - inserted;
for (int j = 0; j < insertnow; j++)
result += filler;
inserted += insertnow;
}
return result;
}

- XGBcM.gif (101.13 KiB) Viewed 1509 times
Re: Justify text in label
Posted: Sat Mar 27, 2021 1:45 pm
by Claudio Ricardo
Muy bueno... habrá que traducirlo a C o tal vez se pueda directamente con funciones similares de Harbour
Yo pensé que necesitabas cómo en el Browse o Grid que se llama Justify pero en realidad es alinear
Lástima que nunca se me dió por aprender C#

Re: Justify text in label
Posted: Sat Mar 27, 2021 6:12 pm
by gfilatov
Claudio Ricardo wrote: ↑Sat Mar 27, 2021 3:18 am
Hola...
Lo sé, pero para lograrlo habría que hacer una función que tomando el width del label y
el tamaño de la fuente, cuente caracteres más espacios, y divida la frase aún en medio
de una palabra, insertando un CRLF cada vez.
Alignment Left es lo más aproximado a eso, sólo que no corta las palabras.
Screenshot_20210326_235600.png
Cuando tenga un dia libre intentaré hacer la función, mientras dejo preparado
un programita para probarla o si alguien del foro desea hacerla.
Pdt. Le puse fondo blanco para que se note mejor
Hola Ricardo,
Please take a look for the updated screen of your sample below:

- capture.jpg (77.33 KiB) Viewed 1477 times
I've replaced a first LABEL control with the RICHEDIT control and add the command
SET CONTROL Label_1 OF Main NOTEDGE
Also I've used the following function call for this label:
_SetFormatJustifyRTF ( "Main" , "Label_1" , .T. )
Note: the above function is available into the MiniguiEx build.
Re: Justify text in label
Posted: Sat Mar 27, 2021 7:24 pm
by gfilatov
Claudio Ricardo wrote: ↑Sat Mar 27, 2021 3:18 am
Hola...
Lo sé, pero para lograrlo habría que hacer una función que tomando el width del label y
el tamaño de la fuente, cuente caracteres más espacios, y divida la frase aún en medio
de una palabra, insertando un CRLF cada vez.
Alignment Left es lo más aproximado a eso, sólo que no corta las palabras.
Screenshot_20210326_235600.png
Cuando tenga un dia libre intentaré hacer la función, mientras dejo preparado
un programita para probarla o si alguien del foro desea hacerla.
Pdt. Le puse fondo blanco para que se note mejor
Hola Ricardo,
There is the following updated source with using RICHEDIT control as Label_1 for HMG
Code: Select all
#include <hmg.ch>
Function Main
Load Window Main
Main.Center
SET CONTROL Label_1 OF Main NOTEDGE
SetProperty ("Main" , "Label_1" , "Value" , "Este vídeo fue una creación audiovisual que se baso en la recopilación de distintos medios visuales para realización de este vídeo. La Ley de Copyright de los Estados Unidos de América especifica que todo vídeo cuyo propósito sea entretenimiento, reportaje, educación, investigación o comentario no infringe los derechos originales de los contenidos y por lo tanto se considera Uso Justo Fair Use bajo la ley estadounidense.")
Main.Label_1.ParaAlignment := RTF_JUSTIFY
SetProperty ("Main" , "Label_2" , "Value" , "Este vídeo fue una creación audiovisual que se baso en la recopilación de distintos medios visuales para realización de este vídeo. La Ley de Copyright de los Estados Unidos de América especifica que todo vídeo cuyo propósito sea entretenimiento, reportaje, educación, investigación o comentario no infringe los derechos originales de los contenidos y por lo tanto se considera Uso Justo Fair Use bajo la ley estadounidense.")
SetProperty ("Main" , "Label_3" , "Value" , "Este vídeo fue una creación audiovisual que se baso en la recopilación de distintos medios visuales para realización de este vídeo. La Ley de Copyright de los Estados Unidos de América especifica que todo vídeo cuyo propósito sea entretenimiento, reportaje, educación, investigación o comentario no infringe los derechos originales de los contenidos y por lo tanto se considera Uso Justo Fair Use bajo la ley estadounidense.")
Main.Activate
Return Nil
Hope that helps

Re: Justify text in label
Posted: Sat Mar 27, 2021 7:56 pm
by danielmaximiliano
Gracias Grigory , siempre tiene la solucion
saludo a la distancia
Re: Justify text in label
Posted: Sat Mar 27, 2021 7:57 pm
by Claudio Ricardo
Muchas Gracias Grigory... me será de mucha utilidad...
Yo lo hice con EditBox en lugar de Label (subi al foro un programa "Utiles" que tiene un par de funciones así)
Pero vi que Daniel necesita que funcione diferente, añadiendo espacios entre letras como en el .gif
Estaba probando de crear una función pero me traba el calcular bién los pixels que ocupan los caracteres
Lo subo, tal vez sirva como base para lo que Daniel necesita.
En la variable cCad poner el texto.
Thank you very much Grigory ... it will be very useful to me ...
I did it with EditBox instead of Label (I uploaded a program "Utiles" that has a couple of functions like this)
But I saw that Daniel needs it to work differently, adding spaces between letters like in the .gif
I was trying to create a function but I was unable to calculate the pixels that the characters occupy
I upload it, maybe it will serve as a basis for what Daniel needs.
In the variable cCad put the text.